将列表中的每个数据帧导出到csv [英] Export each data frame within a list to csv

查看:246
本文介绍了将列表中的每个数据帧导出到csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框列表。每个数据帧具有相同的列名称和不同的行数。我想将我的列表中的每个数据帧导出到自己的csv文件。文件名应为数据框的名称。我已经通过几个论坛帖子尝试不同的建议,但许多都没有工作或给我一个空的csv文件。



这是我目前拥有的代码:



#导出列表中的每个数据框
export = function(data){
filename = function(x)粘贴[[x]]),.csv);
write.csv(data,filename,);
}

export(data)

 data.frame中的错误(column1 = c(1L,2L,3L,4L,:
参数表示不同的行数:62,49,41,55,39,32,34

然后我想,也许我需要使用 data [[x]] 在write.csv中读取:

  write.csv(data [[x]],filename,)

但是这给我的错误is.data.frame(x)中的错误:object'x'not found



这是我的数据框列表的结构。

  str(data)
of 10
$:'data.frame':62 obs。of 6 variables:
.. $ TQ:Factor w / 396 levelsV0Q1,...: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 ...
.. $位置:num [1:62] 0 0.456 2.419 2.63 3.792 ...
.. $ D:系数w / 2级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。的6个变量:
.. $ TQ:因子w / 396级别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 ...
.. $位置:num [1:49] 0.16 5.95 9.87 12.69 15.22 ...
.. $ D:系数w / 2级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。的6个变量:
.. $ TQ:Factor w / 396 levelsV03Q,..: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 ...
.. $位置:num [1:41] 1.3 4.94 7.52 8.34 11.28 ...
.. $ D:系数w / 2级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 ...

有任何建议吗?谢谢。

解决方案

发生错误的原因是您尝试将列表传递给 write.csv ,但它需要一个data.frame。它有助于试图强制列表到一个data.frame,但不是成功的,并告诉你这样。你需要循环遍历列表。有不同的循环可能性,但我喜欢好的旧 for 循环为这样的东西。



(未测试):

  for(i in seq_along(data)){
filename = )[i],.csv)
write.csv(data [[i]],filename)
}



b
$ b

显然你的列表元素没有命名。尝试这样:

  for(i in seq_along(data)){
filename = paste(i, csv)
write.csv(data [[i]],filename)
}


I have a list of data frames. Each data frame has the same column names and a varying number of rows. I would like to export each data frame within my list to its own csv file. The file name should be the name of the data frame. I have looked through several forum posts to try to different suggestions, but many have either not worked or gave me a blank csv file. Most of the posts I viewed were ~4 years old so that may be part of it.

This is the code I currently have:

    #Export each data frame within a list
    export = function(data){
    filename = function(x) paste(names(data[[x]]), ".csv");
    write.csv(data, filename,);
    }

    export(data)

The error I get is

"Error in data.frame(list(Column1 = c(1L, 2L, 3L, 4L, : 
arguments imply differing number of rows: 62, 49, 41, 55, 39, 32, 34

Then I thought, well maybe I need to use data[[x]] in write.csv to read:

    write.csv(data[[x]], filename,)

But that gives me the error "Error in is.data.frame(x): object 'x' not found"

Here is the structure of my list of data frames.

    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 ...

Any suggestions? Thank you.

解决方案

The error occurs because you try to pass a list to write.csv, but it needs a data.frame. It helpfully tries to coerce the list to a data.frame, but isn't sucessfull and tells you so. You need to loop through the list. There are different possibilities to loop, but I prefer the good old for loop for things like this.

Try this (not tested):

for (i in seq_along(data)) {
    filename = paste(names(data)[i], ".csv")
    write.csv(data[[i]], filename)
}

Of course you could wrap this in a function.

Edit:

Apparently you list elements are not named. Try this instead:

for (i in seq_along(data)) {
    filename = paste(i, ".csv")
    write.csv(data[[i]], filename)
}

这篇关于将列表中的每个数据帧导出到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆