1

I have a list, like this:

x <- 2
y <- "sdsd"
z <- 2323322
a_list <- list("x" = x, "y" = y, "z" = z)

> a_list
$x
[1] 2

$y
[1] "sdsd"

$z
[1] 2323322

I want to write this list to a csv file, one row for each element. I tried write.csv(a_list, file = "abc.csv") and got this

    x   y       z
1   2   sdsd    2323322

If transpose the result, it would be what I want:

x, 2
y, sdsd
z, 2323322
Belter
  • 3,573
  • 5
  • 42
  • 58
  • This is a perfectly reasonable question, with sample code and what OP tried. I really see no reason to down-vote. – Roman Luštrik Aug 18 '16 at 09:38
  • @Roman It might be a bit harsh, but I think the OP didn't do much research effort. – llrs Aug 18 '16 at 09:39
  • @Llopis of course one can always try harder, but ROI may quickly diminish if one is not very versed. I think minimal effort was shown and personally I'm satisfied up to the point where I'm ready to help out (posting answer now :) ). – Roman Luštrik Aug 18 '16 at 09:43

3 Answers3

3

Here's my answer,

x <- 2
y <- "sdsd"
z <- 2323322
a_list <- list("x" = x, "y" = y, "z" = z)

write.csv(t(as.data.frame(a_list)), file= "abc.csv")

which I got from the following, https://stackoverflow.com/a/27594632/1945827 answer.

So, change list to data.frame, transpose and write to give,

    V1
x   2
y   sdsd
z   2323322

You may want to do something about the 'V1' column name.

Community
  • 1
  • 1
DarrenRhodes
  • 1,431
  • 2
  • 15
  • 29
2
#Data
x <- 2
y <- "sdsd"
z <- 2323322
a_list <- list("x" = x, "y" = y, "z" = z)

#To show it works
rbind(a_list[1], a_list[2], a_list[3])
     x      
[1,] 2      
[2,] "sdsd" 
[3,] 2323322

#Save
write.csv(rbind(a_list[1], a_list[2], a_list[3]), "data.csv")

You can also add row names:

data<-rbind(a_list[1], a_list[2], a_list[3])
rownames(data)<-c("x,","y,","z,")

Or easier way round:

 write.csv(cbind(a_list), "data.csv")
Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
2

You're close, but you won't be off the hook that easy. I haven't checked the code, but I'm guessing that write.table coerces your list to a data.frame before pushing the contents to a file, as demonstrated here:

> as.data.frame(a_list)
  x    y       z
1 2 sdsd 2323322

This is fine, but you want it in reverse, where columns should be row names. You can use function t() for that.

> t(as.data.frame(a_list))
  [,1]     
x "2"      
y "sdsd"   
z "2323322"

This is now ready to be pushed to a write.table, which can be customized to do your bidding.

Here I explicitly turned row names on (it's already the default) and turned column names off. Quotes are not needed in this case so they're off as well.

write.table(t(as.data.frame(a_list)), 
            file = "myfile.txt", 
            row.names = TRUE, 
            col.names = FALSE,
            quote = FALSE)

Contents of myfile.txt:

x 2
y sdsd
z 2323322

If you are hell-bent on having commas after letters, you can do

out <- t(as.data.frame(a_list))
rownames(out) <- paste(rownames(out), ",", sep = "")

write.table(out, 
            file = "myfile.txt", 
            row.names = TRUE, 
            col.names = FALSE,
            quote = FALSE)

x, 2
y, sdsd
z, 2323322
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • @Belter if any of the answers solved your issue, consider checking the ckecmark below the score to mark it as the accepted answer. – Roman Luštrik Aug 20 '16 at 09:19