我有一个数据框列表。每个数据框都具有相同的列名和不同数量的行。我想将列表中的每个数据框导出到其自己的CSV文件中。文件名应该是数据框的名称。我已经查看了几个论坛帖子,尝试了不同的建议,但很多要么没有起作用,要么给了我一个空的CSV文件。我查看的大部分帖子都是 ~4年前发布的,这可能是其中一部分原因。
以下是我目前拥有的代码:
我遇到的错误是:
有什么建议吗?谢谢。
以下是我目前拥有的代码:
#Export each data frame within a list
export = function(data){
filename = function(x) paste(names(data[[x]]), ".csv");
write.csv(data, filename,);
}
export(data)
我遇到的错误是:
"Error in data.frame(list(Column1 = c(1L, 2L, 3L, 4L, :
arguments imply differing number of rows: 62, 49, 41, 55, 39, 32, 34
我想,也许我需要在write.csv中使用data[[x]]
来读取:
write.csv(data[[x]], filename,)
但是这给了我一个错误“Error in is.data.frame(x): object 'x' not found”
这是我的数据框列表的结构。
str(data)
List of 10
$ :'data.frame': 62 obs. of 6 variables:
..$ TQ: Factor w/ 396 levels "V0Q1",..: 167 168 97 192 2 98 193 194 99 3 ...
..$ CO : int [1:62] 1 1 1 1 1 1 1 1 1 1 ...
..$ PhP : int [1:62] 166576 565275 1091111 1181342 1735982 2237418 2354002 3869267 6155933 7027686 ...
..$ Position : num [1:62] 0 0.456 2.419 2.63 3.792 ...
..$ D : Factor w/ 2 levels "D2012",..: 1 1 1 2 1 1 2 2 1 1 ...
..$ bin_index : int [1:62] 1 1 1 1 1 2 2 2 3 3 ...
$ :'data.frame': 49 obs. of 6 variables:
..$ TQ: Factor w/ 396 levels "V03Q1",..: 227 169 228 229 230 231 232 233 234 235 ...
..$ CO : int [1:49] 2 2 2 2 2 2 2 2 2 2 ...
..$ PhP : int [1:49] 805189 2219895 3386011 3999185 4430767 4644792 5223962 5392366 6052813 6136478 ...
..$ Position : num [1:49] 0.16 5.95 9.87 12.69 15.22 ...
..$ D : Factor w/ 2 levels "D2012",..: 2 1 2 2 2 2 2 2 2 2 ...
..$ bin_index : int [1:49] 1 2 2 3 4 4 4 4 5 5 ...
$ :'data.frame': 41 obs. of 6 variables:
..$ TQ: Factor w/ 396 levels "V03Q",..: 28 257 258 105 144 29 106 30 145 259 ...
..$ CO : int [1:41] 3 3 3 3 3 3 3 3 3 3 ...
..$ PhP : int [1:41] 1055462 2022884 2222474 2269935 3497149 3755007 5079223 6855634 7091838 10284891 ...
..$ Position : num [1:41] 1.3 4.94 7.52 8.34 11.28 ...
..$ D : Factor w/ 2 levels "D2012",..: 1 2 2 1 1 1 1 1 1 2 ...
..$ bin_index : int [1:41] 1 1 2 2 3 3 4 5 6 8 ...
有什么建议吗?谢谢。
str(data)
。 - Roland