Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I am forming a data frame where one column is named as a major, and the second column is the list of courses in the major.

df <- data.frame(majors = majors)

df[,"courses"] <- courses

where majors are a vector and courses is a list of lists. When I try to write using

write.csv(df,"Majors_and_Courses.csv", row.names = FALSE)

I get an error:

"Error in write.table(df, "Majors_and_Courses.csv", row.names = FALSE, : unimplemented type 'list' in 'EncodeElement'"

1 Answer

0 votes
by (108k points)

The solution is to use the data.table:

library(data.table)

dt <- data.table(majors = c("John", "Lisa", "Tim"))

courses <- list(a = list("m","n","o"), b = list("p", "q", "r"), c = list("s", "t", "u"))

dt[,"courses"] <- courses

dt$courses <- vapply(dt$courses, paste, collapse = "; ", character(1L))

write.csv(dt, "Majors_and_Courses.csv", row.names = FALSE)

If you are interested in R certification, then do check out the R programming certification.

Browse Categories

...