Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在循环的每次迭代中更改变量名?_R - Fatal编程技术网

R 如何在循环的每次迭代中更改变量名?

R 如何在循环的每次迭代中更改变量名?,r,R,我试图使用循环来简化一些代码,这样我就可以分析一个文件夹中的多组数据,而不必为每个数据集编写新的代码 此代码的目标是将文件夹中的每个.csv文件加载到其自己的数据帧中,并相应地命名 #Define the path to the folder containing the data sets folder <- "Volumes/DataHD/Folder/" #Make a list of the files within that folder files <- list.fi

我试图使用循环来简化一些代码,这样我就可以分析一个文件夹中的多组数据,而不必为每个数据集编写新的代码

此代码的目标是将文件夹中的每个.csv文件加载到其自己的数据帧中,并相应地命名

#Define the path to the folder containing the data sets
folder <- "Volumes/DataHD/Folder/"

#Make a list of the files within that folder
files <- list.files(path = folder)

#Define the desired names of each dataframe
names <- c("A1", "A2", "A3")

#Set working directory to that folder
setwd(folder)

#Use a for loop to load each .csv file into its own dataframe
i = 1

for(theFile in files){
    name[i] <- read.csv(theFile)
    i = i + 1 
}
#定义包含数据集的文件夹的路径

文件夹您可能需要从基本目录使用
assign()
函数

for(theFile in files){
  assign(name[i], read.csv(theFile))
  i = i + 1 
}
或者,您可能希望对每个文件使用
lappy()
setNames()
,然后使用
list2env()
设置您的环境。这样就不需要使用
for()
循环和i计数器

list2env(lapply(setNames(files, names), 
         read.csv), envir = .GlobalEnv)
如果你觉得它更可读,你也可以用管道来写

setNames(files, names) %>% lapply(., read.csv)  %>% list2env(., envir = .GlobalEnv)

我认为一个更干净的方法是lappy和read.csv

#Define the path to the folder containing the data sets
folder <- "Volumes/DataHD/Folder/"

#Make a list of the files within that folder
files <- list.files(path = folder)

#Define the desired names of each dataframe
names <- c("A1", "A2", "A3")

#Set working directory to that folder
setwd(folder)

# read all files
df_list <- lapply(files, read.csv)

# set the names of your list
names(df_list) <- names
#定义包含数据集的文件夹的路径

文件夹我不知道,但听起来你需要一组数据帧。一个数组或列表可能就可以了。您需要
assign
assign(name[i],read.csv(theFile))
@RonakShah非常感谢!为了让它工作,我必须执行assign(toString(name[I]),read.csv(theFile)),但assign函数正是我所需要的!为了简化您的代码,。非常感谢!为了让它工作,我必须执行assign(toString(name[I]),read.csv(theFile)),但assign函数正是我所需要的!我想知道在你的情况下toString()有什么必要?您提供的示例“名称”在我这方面效果很好。
list2env(lapply(setNames(files, names), 
         read.csv), envir = .GlobalEnv)
setNames(files, names) %>% lapply(., read.csv)  %>% list2env(., envir = .GlobalEnv)
#Define the path to the folder containing the data sets
folder <- "Volumes/DataHD/Folder/"

#Make a list of the files within that folder
files <- list.files(path = folder)

#Define the desired names of each dataframe
names <- c("A1", "A2", "A3")

#Set working directory to that folder
setwd(folder)

# read all files
df_list <- lapply(files, read.csv)

# set the names of your list
names(df_list) <- names