1

I have a data frame with some entries as lists. This was an import from a JSON file, where an entry might have multiple tags. It imported JSON file using jsonlite package with flatten=TRUE. An example entry from my tags column is:

list(tag = c("ethicaltheory", "gametheory"), raw_tag = c("ethical heory", "Game Theory"))

I filtered the table down and want to export it to a csv. When I tried the write.csv command, I hit an error when it hit the first entry with list:

"unimplemented type 'list' in 'EncodeElement'"

The question is can I export this file as is, did I make a mistake in importing it?

I'd be fine with converting entries to strings or something, but I'm not sure how to do that for the entire table.

Artem
  • 3,304
  • 3
  • 18
  • 41
Josh
  • 13
  • 5
  • 1
    What exactly do you want the output file to look like? CSV files expect to have each column be a single value and every row to have the same number of columns. How many columns have multiple values? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 14 '18 at 19:14
  • I'd be fine with the tag column having each entry literally just be "list(tag = c("ethicaltheory", "gametheory"), raw_tag = c("ethical theory", "Game Theory"))" etc. I'll append my entry with an example. – Josh Jun 14 '18 at 19:22
  • OK. But you are going to have a heck of a time reading that back into R because that's certainly not a "normal" CSV file. Are you sure you really need CSV? What if you stored it in a binary format that R can more easily read? – MrFlick Jun 14 '18 at 19:24
  • I wanted something I could share with people who don't know R. Actually, I couldn't create an example, because R won't let me do what the read_json command did from scratch. – Josh Jun 14 '18 at 19:30
  • I think I figured out a fix assets_refined <- data.frame(lapply(assets_filtered_more, as.character), stringsAsFactors = FALSE) – Josh Jun 14 '18 at 19:30
  • Ugly, But I think it works. Where assets_filtered_more was my filtered dataframe from the original assets that jsonlite read in. – Josh Jun 14 '18 at 19:32

1 Answers1

0

Interesting problem. I did not know that data.frame can store list. This algorithm is taking variable length list stored in a row of a data.frame. Then it finds maximum length, create matrix with proper dimensions then it saves the file.

x <- list(tag = letters[1:2], raw_tag = letters[1:3])
y <- list(tag = letters[1:2], raw_tag = letters[1:2])
z <- list(tag = letters[1:3], raw_tag = letters[1:4])
df <- data.frame(clmn = I(list(x, y, z)))

r <- apply(df, 1, unlist)
lm <- max(unlist(lapply(r, length)))

df <- data.frame(
  matrix(
    rep(0, (lm * nrow(df))),
    ncol = lm
    )
  )
)

vals.v <- unlist(lapply(1:nrow(df), function(i) {
  v <- unlist(r[i])
  l <- length(v)
  c(v, rep(0, lm - l))
}))
fin.res <- t(matrix(vals.v, ncol = nrow(df)))

# write.csv(fin.res, "res2.csv") # uncomment to save CSV file
Artem
  • 3,304
  • 3
  • 18
  • 41
  • I didn't either. I've played around with it, but it's hard to reproduce from scratch. Somehow the json reader knew how. – Josh Jun 15 '18 at 00:21
  • That seems to do the trick, in terms of expanding out that sub-list – Josh Jun 15 '18 at 00:21