读取多个CSV并在R中的循环中添加到列表
我有多个CSV文件,希望在R中读取它们。文件名作为参数提供,因此我事先不知道名称。这就是为什么我要做一个循环 接下来,我希望将每个数据帧附加到一个列表中。因此,我希望有一个索引列表来访问所有数据帧的第一行,我认为最好的方法是有一个可用的数据帧列表。如果有更有效的方法做到这一点,任何解决方案都是值得赞赏的 但现在,我将使用列表方法来获得所有数据帧。为此,我想知道如何将数据帧添加到循环中的列表中 我有以下代码:读取多个CSV并在R中的循环中添加到列表,r,csv,dataframe,R,Csv,Dataframe,我有多个CSV文件,希望在R中读取它们。文件名作为参数提供,因此我事先不知道名称。这就是为什么我要做一个循环 接下来,我希望将每个数据帧附加到一个列表中。因此,我希望有一个索引列表来访问所有数据帧的第一行,我认为最好的方法是有一个可用的数据帧列表。如果有更有效的方法做到这一点,任何解决方案都是值得赞赏的 但现在,我将使用列表方法来获得所有数据帧。为此,我想知道如何将数据帧添加到循环中的列表中 我有以下代码: #splitting the args to get the filenames spl
#splitting the args to get the filenames
splat <- strsplit(args[1], ",")[[1]]
for (fname in splat) {
#d.fname = dataframe
d.fname <- read.csv(fname, header = FALSE, sep = ",", dec = ".")
#code needed to add d.fname to a list?
}
#拆分参数以获取文件名
splat对于原始问题,您可以直接使用:
lapply(strsplit(args[1], ",")[[1]], read.csv, header=F, sep=',', dec='.')
对于原始问题,您可以直接使用:
lapply(strsplit(args[1], ",")[[1]], read.csv, header=F, sep=',', dec='.')
要回答问题的第一部分,可以使用
# Create an empty list
csvList <- list()
#splitting the args to get the filenames
splat <- strsplit(args[1], ",")[[1]]
for (fname in splat) {
#d.fname = dataframe
d.fname <- read.csv(fname, header = FALSE, sep = ",", dec = ".")
#code needed to add d.fname to a list?
csvList[[length(csvList)+1]] <- d.fname
}
#创建一个空列表
csvList要回答问题的第一部分,可以使用
# Create an empty list
csvList <- list()
#splitting the args to get the filenames
splat <- strsplit(args[1], ",")[[1]]
for (fname in splat) {
#d.fname = dataframe
d.fname <- read.csv(fname, header = FALSE, sep = ",", dec = ".")
#code needed to add d.fname to a list?
csvList[[length(csvList)+1]] <- d.fname
}
#创建一个空列表
csvList这可能也有帮助
# instantiate an empty list
ldf <- list()
# creates the list of all the csv files in the directory
# filter for filenames holding UnixM
listcsv <- dir(pattern = ".UnixM.")
# loop thorugh the list holding the single CSV files
for (k in 1:length(listcsv)){
ldf[[k]] <- read.csv(file = listcsv[k])
}
#实例化一个空列表
ldf这可能也有帮助
# instantiate an empty list
ldf <- list()
# creates the list of all the csv files in the directory
# filter for filenames holding UnixM
listcsv <- dir(pattern = ".UnixM.")
# loop thorugh the list holding the single CSV files
for (k in 1:length(listcsv)){
ldf[[k]] <- read.csv(file = listcsv[k])
}
#实例化一个空列表
ldf您可以通过csvNames=strsplit(args[1],“,”[[1]])
继续,然后您可以简单地执行lappy(csvNames,read.csv,header=F,sep=',dec=')
。下一个问题是:如何访问列表中每个数据帧的第一行的第一个元素?可能是lappy(lst,function(x)x[1,1]
lappy(seq__-on(lst)function(x)lst[[x]][x,1])
…但首先尝试测试和理解lappy;)您可以通过csvNames=strsplit args[1],“,”,”[[1]]
然后您可以简单地执行lappy(csvNames,read.csv,header=F,sep=',',dec=')
好的。可以作为解决方案imho输入。下一个问题是:如何访问列表中每个数据帧的第一行的第一个元素?可能是lappy(lst,function(x)x[1,1]
lappy(seq_沿着(lst)function(x)lst[[x]][x,1])
…但首先尝试测试和理解lappy;)