Export each frame of data to a list in csv

I have a list of data frames. Each data frame has the same column names and a different number of rows. I would like to export each frame of data to my list in my own csv file. The file name must be the name of the data frame. I looked through several forum posts to try different suggestions, but many of them either didn't work or gave me an empty csv file. Most of the posts I looked at were ~ 4 years old to be part of this.

This is the code I 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 that maybe I need to use data[[x]]write.csv for reading:

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

But it gives me an 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? Thanks.

+4
1

- , write.csv, data.frame. data.frame, . . , for .

( ):

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

, .

Edit:

-, . :

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

All Articles