Home > Mobile >  How to save a List of Lists to a CSV file in R?
How to save a List of Lists to a CSV file in R?

Time:12-21

I have a list of lists stored in a variable containing 864 items, which looks like this.

-> allpermsGlob

[[1]]
[[1]][[1]]
[1] "Agent is selling "
        
[[1]][[2]]
[1] "Yes, if asked to subscribe "
        
[[1]][[3]]
[1] "Yes, if offered the two-year option "
        
[[1]][[4]]
[1] "No, Agent should offer"
        
[[1]][[5]]
[1] "No, it is never ethical"
.
.
.
.
.
[[864]]
[[864]][[1]]
[1] "Agent is selling "

[[864]][[2]]
[1] "Yes, if asked to subscribe"

[[864]][[3]]
[1] "Yes, if offered the two-year "

[[864]][[4]]
[1] "No, Agent should offer "

[[864]][[5]]
[1] "No, it is never ethical 
    

I need the output in to be stored in a csv file with 2 columns. The 1st column replaces square brackets to Stem & 4 options as shown in example below & 2nd column should contain corresponding values for all 864 items & should look like below for all the 864 items

    'Stem',"Agent is selling  subscriptions "
    
    "Option 1",
    "Yes, if asked to subscribe"
    
    "Option 2",
    "Yes, if offered the two-year option "
    
    "Option 3",
    "No, Agent should offer "
    
    "Option 4",
    "No, it is never ethical 

How do i achieve this?

Doing dput(head(yourlist)) gives-

list(list("Agent is selling subscriptions ", 
    "Yes, if offered the two-year option", 
    "Yes, if asked to subscribe ", 
    "No, Agent should offer ", 
    "No, it is never ethical"), 
    list("Agent is selling subscriptions", 
        "Yes, if offered the two-year option", 
        "Yes, if asked to subscribe ", 
        "No, it is never ethical ", 
        "No, Agent should offer "))

CodePudding user response:

Here's a small example:

> x <- list(as.list(letters[1:5])) #same structure as OP, different text
> print(x)
[[1]]
[[1]][[1]]
[1] "a"

[[1]][[2]]
[1] "b"

[[1]][[3]]
[1] "c"

[[1]][[4]]
[1] "d"

[[1]][[5]]
[1] "e"

> y <- as.data.frame(x)

> y
  X.a. X.b. X.c. X.d. X.e.
1    a    b    c    d    e

> names(y) <- c("stem", "opt1", "opt2", "opt3", "opt4")

> y
  stem opt1 opt2 opt3 opt4
1    a    b    c    d    e

Then you can use write.csv() to export y to CSV. Not sure how you want to have y as a data frame and still get it printed like you showed. Maybe you just want it to be a named list? Maybe this will get you closer to what you're looking for as output:

> sapply(y, function(i) list(i))
$stem
[1] "a"

$opt1
[1] "b"

$opt2
[1] "c"

$opt3
[1] "d"

$opt4
[1] "e"


Depending on how large your project is, you may also consider creating x as a special case of the list class with its own print method.

CodePudding user response:

Assuming the example mimic your data structure, here is a possible solution:

# the structure of your object
ex_list <- list(list("a", "b", "c"),
                list("a", "b", "c"))
print(ex_list)
#> [[1]]
#> [[1]][[1]]
#> [1] "a"
#> 
#> [[1]][[2]]
#> [1] "b"
#> 
#> [[1]][[3]]
#> [1] "c"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] "a"
#> 
#> [[2]][[2]]
#> [1] "b"
#> 
#> [[2]][[3]]
#> [1] "c"
# give names to each otion a, b and c
ex_list <- lapply(ex_list, function(x) setNames(x, c("stem", "opt1", "opt2")))
# merge everything in a data.frame

temp <- unlist(ex_list)

out <- data.frame(ID = rep(1:length(ex_list), lengths(ex_list)), 
           Options = names(temp), 
           Values = unlist(temp))
print(out)
#>   ID Options Values
#> 1  1    stem      a
#> 2  1    opt1      b
#> 3  1    opt2      c
#> 4  2    stem      a
#> 5  2    opt1      b
#> 6  2    opt2      c

Created on 2022-12-20 with reprex v2.0.2

Then you can use write.csv() with the object out to get a CSV file.

Ideas from: https://www.r-bloggers.com/2021/12/an-easy-to-convert-list-to-long-table/

CodePudding user response:

We could just use cbind's recycling abilities if all lists are complete:

cbind(Options = c("Stem1", "Option1", "Option2", "Option3", "Option4"), Values = unlist(allpermsGlob)) |>
    write.csv("test.xls", row.names = FALSE)

Output (csv):

"Options","Values"
"Stem1","Agent is selling subscriptions "
"Option1","Yes, if offered the two-year option"
"Option2","Yes, if asked to subscribe "
"Option3","No, Agent should offer "
"Option4","No, it is never ethical"
"Stem1","Agent is selling subscriptions"
"Option1","Yes, if offered the two-year option"
"Option2","Yes, if asked to subscribe "
"Option3","No, it is never ethical "
"Option4","No, Agent should offer "
  • Related