如何在R中将列表的列表保存为CSV文件?

fafcakar  于 2022-12-25  发布在  其他
关注(0)|答案(3)|浏览(252)

我有一个列表的列表存储在一个包含864项的变量中,它看起来像这样。

  • 〉所有权限全局
[[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
    • 我需要将中的输出存储在包含2列的csv文件中**.第1列将方括号替换为词干和4个选项,如下例所示&第2列应包含所有864个项目的相应值&所有864个项目应如下所示
'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

我该如何实现这一目标?
做dput(head(yourlist))可以得到-

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 "))
ggazkfy8

ggazkfy81#

下面是一个小例子:

> 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

然后你可以使用write.csv()y导出到CSV。不确定你想如何将y作为数据框 * 并且 * 仍然可以像你展示的那样打印它。也许你只是想让它成为一个命名列表?也许这会让你更接近你想要的输出:

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

$opt1
[1] "b"

$opt2
[1] "c"

$opt3
[1] "d"

$opt4
[1] "e"

根据项目的大小,您还可以考虑创建x作为list类的特例,并使用其自己的print方法。

flseospp

flseospp2#

假设示例模拟了您的数据结构,下面是一个可能的解决方案:

# 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

创建于2022年12月20日,使用reprex v2.0.2
然后,您可以使用write.csv()和对象out来获取CSV文件。
想法来源:https://www.r-bloggers.com/2021/12/an-easy-to-convert-list-to-long-table/

r55awzrz

r55awzrz3#

如果所有列表都完成了,我们可以使用cbind的回收能力:

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

输出(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 "

相关问题