0 votes
1 view
in R Programming by (36k 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 (78.1k points)

The solution is to use the 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !