2

I have a long list ("mylist") in R containing sequences of characters that I want to export to a .csv such that each cell contains 1 character:

> str(mylist)
List of 1
 $ :List of 259
  ..$ : chr [1:214] "A" "B" "C" "D" ...
  ..$ : chr [1:220] "E" "F" "G" "H" ...

When I use write.csv on this, I get an error because the lists contain different numbers of rows. Fine. So I try:

mynewlist<-do.call("cbind", mylist)
write.csv(t(mynewlist), file="mynewlist.csv", quote = FALSE)

but I get

    V1        V2     V3       V4    V5
1   c("A"    "B"     "C"     "D"    ...

as my output when loaded in excel, which still contains double quotes as well as the "c(" seen in the first cell. Furthermore, though the first row of the CSV should have 214 columns, it wraps back to a new row after 101 cells. The write.csv and write.table functions seem pretty straightforward, as does my data set, so I'm not sure what's going on here. Any thoughts why I can't get a clean CSV export? Thanks!

BillG
  • 23
  • 2
  • 1
    You should make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Give a specific sample of `mylist` (a `dput()` would be helpful) and write out the desired output for that sample input. – MrFlick Jul 24 '15 at 21:17
  • Desired output is as shown in the last code box, but no quotation marks, no "c(" in V1,1 and no wrapping of rows as described. – BillG Jul 24 '15 at 22:00

2 Answers2

0

write.table only cares for the data format when you use it to write all lines at once. You can simply wrap it in a loop:

mylist <- list(sample(letters,10), sample(letters,20))

# write in a loop
for(i in mylist) write.table(t(i), "mytext.csv", quote=F, append=T, 
                 col.names=F, row.names=F)

Content of resulting file:

n v w x d y o k u m
w p z d x o u i s h y r f b c l e k m v

Note that append=T will not warn you if that file already exists, so better check it if necessary.

SimonG
  • 4,701
  • 3
  • 20
  • 31
  • Thanks! This does work, though it seems like there should be something simpler that should work as well that doesn't require looping and requiring to append. – BillG Jul 24 '15 at 21:59
0

It is not really clear what you want to achieve. With write.csv() you will want to have a data.frame first, but you have a list that consists of vectors of strings of different length.

If you just want to write all characters of one list item in one line, separated by a comma, here is what I would use:

for( i in mylist ) cat( file = "myfile.csv", i, "\n", sep = ",", append = TRUE )

If you want to build a data.frame first that you then can save to file with write.csv(), things are a bit more complicated. The advantage could be (depending what your next steps are) that all lines have the same length.

# get max length of of list items
longest <- max( sapply( mylist, length ) )
# initialize data.frame
myDF <- NULL
# loop through your list and make it a data.frame (matrix, actually)
for( i in 1 : length( mylist ) )
{
  # fill up with empty strings
  empty <- longest - length( mylist[[ i ]] )
  mylist[[ i ]] <- c( mylist[[ i ]], rep( "", empty ) )
  # append to data.frame
  myDF <- rbind( myDF, mylist[[ i ]] )
}
# now you can juse write.csv()
write.csv( myDF, "myfile.csv" )

I guess this is overkill, but just in case...

vaettchen
  • 7,299
  • 22
  • 41