rbind in a list in r(文本到列)

lnlaulya  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(143)

我在一个目录中有3个.csv文件。数据集有标题和text; text; text;

`list_data <- list("Red; Green", 
                  "Green; Blue",
                  "Blue; Yellow")
print(list_data)`

我想把它们合并到1.csv基于相同的标题。

files  <- list.files(pattern = '\\.csv')
tables <- lapply(files, read.csv, header = TRUE)
combined.df <- do.call(rbind , tables)
str(combined.df)

如何解决这个问题?非常感谢。

eni9jsuy

eni9jsuy1#

对真实的数据做一些假设,我将稍微改变样本数据:

list_data <- list(file1="Red; Green\n1;2", file2="Green; Blue\n3;4", file3="Blue; Yellow\n5;6")

我添加了名称以启用在数据行中保留文件名的选项(远低于)。

# tables <- lapply(setNames(nm = files), read.csv2) # what you call with a file list
tables <- lapply(list_data, function(z) read.csv2(text = z))
str(tables)
# List of 3
#  $ file1:'data.frame':    1 obs. of  2 variables:
#   ..$ Red  : int 1
#   ..$ Green: int 2
#  $ file2:'data.frame':    1 obs. of  2 variables:
#   ..$ Green: int 3
#   ..$ Blue : int 4
#  $ file3:'data.frame':    1 obs. of  2 variables:
#   ..$ Blue  : int 5
#   ..$ Yellow: int 6
dplyr::bind_rows(tables)
#   Red Green Blue Yellow
# 1   1     2   NA     NA
# 2  NA     3    4     NA
# 3  NA    NA    5      6
data.table::rbindlist(tables, fill = TRUE, use.name = TRUE)
#      Red Green  Blue Yellow
#    <int> <int> <int>  <int>
# 1:     1     2    NA     NA
# 2:    NA     3     4     NA
# 3:    NA    NA     5      6

如果要保留每行的文件名,则

dplyr::bind_rows(tables, .id = "filename")
#   filename Red Green Blue Yellow
# 1    file1   1     2   NA     NA
# 2    file2  NA     3    4     NA
# 3    file3  NA    NA    5      6
data.table::rbindlist(tables, fill = TRUE, use.name = TRUE, idcol = "filename")
#    filename   Red Green  Blue Yellow
#      <char> <int> <int> <int>  <int>
# 1:    file1     1     2    NA     NA
# 2:    file2    NA     3     4     NA
# 3:    file3    NA    NA     5      6

相关问题