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 "))
7
  • 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. Commented Dec 20, 2022 at 13:23
  • 1
    Does this answer your question? Convert a list to a data frame Commented Dec 20, 2022 at 13:25
  • Another possible solution: stackoverflow.com/q/26177565/10264278 Commented Dec 20, 2022 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? Commented Dec 20, 2022 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 Commented Dec 20, 2022 at 13:53

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Waldir, thanks for the response. I forgot to mention that i have 864 items like that. I have updated the question.
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/

Comments

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 "

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.