1

I have a dataframe that contains lists as shown

dfg <- structure(list(a = list(c("BD", "AS", "MS", 
                                                 "CMO")), b = list(list(width = 757.5, height = 500L))), row.names = "x1", class = "data.frame")

But I am unable to save this in csv fie

write.csv(dfg, "d.csv")
Error in write.table(dfg, "d.csv", col.names = NA, sep = ",", dec = ".",  : 
  unimplemented type 'list' in 'EncodeElement'

Is there a way to save this?

manu p
  • 952
  • 4
  • 10
  • 2
    You can't save nested lists like this in a CSV file. CSV files are just for simple rectangular data. I mean, you could convert that column to a character representation, but then you'd have to parse specially when you read it in. There's no csv reader that will automatically parse complex objects like that (since they aren't expected to be there in the first place.) Maybe consider a different file format like RDS which can preserve R objects. – MrFlick May 12 '22 at 13:07
  • 2
    To keep the data structure, use [`saveRDS`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/readRDS.html) instead. – Rui Barradas May 12 '22 at 13:07

0 Answers0