1

I don't know if is possible...

I would save the file to import it in others future sessions.

I wanna save this data.frame in a csv file (it's a list of list containing vectors):

> operacions<-list(list(Nom="Victor",Bolis= c("Negro","azul","verde")),list(Nom="Dani",Lapices=c(1:4)))
> operacions
[[1]]
[[1]]$Nom
[1] "Victor"

[[1]]$Bolis
[1] "Negro" "azul"  "verde"


[[2]]
[[2]]$Nom
[1] "Dani"

[[2]]$Lapices
[1] 1 2 3 4

But it give me an error:

> write.csv2(operacions, "file.csv",row.names = FALSE)
Error in data.frame(list(Nom = "Victor", Bolis = c("Negro", "azul", "verde" : 
  arguments imply differing number of rows: 3, 4
2
  • 1
    This is not a data frame, this is a list of lists. The problem here is that the elements have differing lengths.. rapply(operacions, length) returns 1 3 1 4. This is a problem, because .csv requires fixed lengths. What would the expected format of the output be here? Commented Apr 25, 2016 at 23:34
  • This may answer your question: stackoverflow.com/a/27594769/5805670 Commented Apr 25, 2016 at 23:40

2 Answers 2

2

Since it's not a data.frame , and more specifically cannot be coerced to one with as.data.frame which is where that message came from, you need to think of a different method of saving the data. Probably this simplest would be with dput, which writes an ASCII representation of the list structure:

dput(operacions, file="out.txt")

To bring it back into R:

new <- source("out.txt")

Another method would be to convert to JSON format, which would also preserve the key-value information rather than just writing the values:

library(jsonlite)
 toJSON(new)
# value---------
{"value":[{"Nom":["Victor"],"Bolis":["Negro","azul","verde"]},{"Nom":["Dani"],"Lapices":[1,2,3,4]}],"visible":[true]} 

You can use the cat function to direct this to a text file:

cat( toJSON(new), file="test.3.txt")
Sign up to request clarification or add additional context in comments.

Comments

0

R Data frames and R lists are potentially incompatible...

A list is, essentially a vector where each element can be of a different type and length of elements. Meanwhile, a Data frame can be thought of a uniform spreadsheet where each column can store a different type of data, or nonnested lists where each element is of the same length. This last point is important as lists can have unequal lengths, whilst a Data frames do not.

Thus, it may be easier to save the list as an RDS file using saveRDS()

dataDir <- "."
saveRDS( operacions, file.path(dataDir, "files"))
operacions2 <- readRDS(file.path(dataDir, "files"))

this will save it as an R-Object, and similarly, restore the R object later.

If you do want to convert to a data frame you can first convert to a nested data frame, and then fill in the blanks in the data frame. Thus...

require(data.table)
require(plyr)


operacions<-list(list(Nom="Victor",Bolis= c("Negro","azul","verde")),list(Nom="Dani",Lapices=c(1:4)))
str(operacions)
operacionsdf <- lapply(operacions, data.frame, stringsAsFactors = FALSE)
str(operacionsdf)

operacionsdf2 <- rbind.fill(operacionsdf)
str(operacionsdf2)
write.csv2(operacionsdf2, "file.csv",row.names = FALSE)
operacionsdf3 <- read.csv2("file.csv")
str(operacionsdf3)

output:

> require(data.table)
> require(plyr)
> operacions<-list(list(Nom="Victor",Bolis= c("Negro","azul","verde")),list(Nom="Dani",Lapices=c(1:4)))
> str(operacions)
List of 2
 $ :List of 2
  ..$ Nom  : chr "Victor"
  ..$ Bolis: chr [1:3] "Negro" "azul" "verde"
 $ :List of 2
  ..$ Nom    : chr "Dani"
  ..$ Lapices: int [1:4] 1 2 3 4
> operacionsdf <- lapply(operacions, data.frame, stringsAsFactors = FALSE)
> str(operacionsdf)
List of 2
 $ :'data.frame':   3 obs. of  2 variables:
  ..$ Nom  : chr [1:3] "Victor" "Victor" "Victor"
  ..$ Bolis: chr [1:3] "Negro" "azul" "verde"
 $ :'data.frame':   4 obs. of  2 variables:
  ..$ Nom    : chr [1:4] "Dani" "Dani" "Dani" "Dani"
  ..$ Lapices: int [1:4] 1 2 3 4
> operacionsdf2 <- rbind.fill(operacionsdf)
> str(operacionsdf2)
'data.frame':   7 obs. of  3 variables:
 $ Nom    : chr  "Victor" "Victor" "Victor" "Dani" ...
 $ Bolis  : chr  "Negro" "azul" "verde" NA ...
 $ Lapices: int  NA NA NA 1 2 3 4
> write.csv2(operacionsdf2, "file.csv",row.names = FALSE)
> operacionsdf3 <- read.csv2("file.csv")
> str(operacionsdf3)
'data.frame':   7 obs. of  3 variables:
 $ Nom    : Factor w/ 2 levels "Dani","Victor": 2 2 2 1 1 1 1
 $ Bolis  : Factor w/ 3 levels "azul","Negro",..: 2 1 3 NA NA NA NA
 $ Lapices: int  NA NA NA 1 2 3 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.