我有一个包含864项的列表列表存储在一个变量中,如下所示:
→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
我需要输出存储在一个csv文件与2列.第一列将方括号替换为Stem &4个选项,如下例所示&第二列应包含所有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 "))
下面是一个小例子:
> 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方法。
假设这个示例模拟了您的数据结构,下面是一个可能的解决方案:
# 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/
如果所有列表都是完整的,我们可以使用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 "