0

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 "))
r_learner
  • 13
  • 3
vin28vi
  • 1
  • 2
  • 1
    Hi, please help us help you by providing a reproducible example and a sample of expected output. For example, use `dput(head(yourlist))` in the console and copy-paste the output in your post. – Paul Dec 20 '22 at 13:23
  • 1
    Does this answer your question? [Convert a list to a data frame](https://stackoverflow.com/questions/4227223/convert-a-list-to-a-data-frame) – Paul Dec 20 '22 at 13:25
  • Another possible solution: https://stackoverflow.com/q/26177565/10264278 – Paul Dec 20 '22 at 13:27
  • Your desired output looks like neither a data frame nor a csv. Do you just want a text file, that looks like that? It's just showing one element of the outer list. How should the output take account of the outer list structure? – shs Dec 20 '22 at 13:52
  • Please edit your question to provide the full `dput(head(yourlist))` and not end in `...` and the structure of your desired output – jpsmith Dec 20 '22 at 13:53
  • i mean that output should be in a csv file, where there are 2 columns. 1st column contains -->Stem1, Option1, Option2, Option3, Option4, Stem2, Option1....Option4 till Stem864....Option4. The 2nd column should contain the Corresponding values. – vin28vi Dec 20 '22 at 14:04
  • @vin28vi please, have a look at [how to make great R reproducible examples?](https://stackoverflow.com/a/5963610/10264278) this might give insights why we are so obsessed with reproducibility. The main point is that we need to be able to run your code. Your question, at the moment, has no runnable code. Just try open a fresh R session and try solving you question with only the content of the post. – Paul Dec 20 '22 at 14:29

3 Answers3

2

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.

Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
  • Hi Waldir, thanks for the response. I forgot to mention that i have 864 items like that. I have updated the question. – vin28vi Dec 20 '22 at 13:43
1

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/

Paul
  • 2,850
  • 1
  • 12
  • 37
0

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 "
harre
  • 7,081
  • 2
  • 16
  • 28