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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With