bnjmn
bnjmn

Reputation: 4584

R: Output data frame with list to csv

I have the following data frame (info) that looks like this:

    > info[1:5,]
                  field    BinningMethod DataType numLevels cumLevel                                            factLevels
    1          data_len       EQUAL AREA   DOUBLE         5        5 (-inf,2.0], (2.0,6.0), [6.0,8.0), [8.0,+inf), MISSING
    2  dns_count_add_rr DISCRETE MAPPING   DOUBLE         3        8                                     0.0, 1.0, MISSING
    3 dns_count_answers DISCRETE MAPPING   DOUBLE         3       11                                     0.0, 1.0, MISSING
    4 dns_count_auth_rr DISCRETE MAPPING   DOUBLE         3       14                                     0.0, 1.0, MISSING
    5 dns_count_queries DISCRETE MAPPING   DOUBLE         2       16                                          1.0, MISSING

With class types:

    > sapply(info, class)
    field BinningMethod      DataType     numLevels      cumLevel    factLevels 
    "character"   "character"   "character"     "numeric"     "numeric"        "list" 

I'd like to output 'info' to a CSV file but do not know how to handle the list field (factLevels). I currently get the following error:

> write.csv( info, 
+ file = paste("FIELDS_", modelFile, sep=""), 
+ row.names = FALSE, na = "")
Error in write.table(x, file, nrow(x), p, rnames, sep, eol, na, dec, as.integer(quote), : unimplemented type 'list' in 'EncodeElement'

What are some possible solutions to this? The only requirement I have is for a java program to be able to read it in and distinguish the different values.

Upvotes: 2

Views: 3465

Answers (1)

joran
joran

Reputation: 173517

I see that @Seb has linked (now deleted) to an answer of mine that is loosely on this topic. (Generally speaking, columns of data frames shouldn't be lists in R.) However, if your only purpose is to dump this information into a file, perhaps this will be more relevant to you:

One simple option may be to convert the factLevels column from a list to a character vector by pasting the values together (using a delimiter other than a comma, of course). Perhaps something like:

info$factLevels <- sapply(info$factLevels,
                          FUN = paste,collapse = "-")

Then you'll have to adjust your java program to parse the factor levels properly, of course.

Upvotes: 3

Related Questions