[英]Combine lists into a single list in R
I have seperate lists such that:我有单独的列表,例如:
1. $text = "I love this product"
2. $text = "Amazing service and care given, will visit once again"
3. $text = "Love this product!!"
How could i merge all lists into one list such that:我怎样才能将所有列表合并到一个列表中,这样:
$text = ["I love this product", "Amazing service and care given, will visit once again", "Love this product!!"]
I would like to save in in a dataframe such that我想保存在 dataframe 中,这样
Product Reviews
A ["I love this product", "Amazing service and care given, will visit once again", "Love this product!!"]
B ["I ......."]
dput
of the list: dput
的输入:
list(list(text="I love this product"),
list(text="Amazing service and care given, will visit once again"),
list(text="Love this product!!"))
When I used cbind, it returns:当我使用 cbind 时,它返回:
Reviews
"I love this product"
"Amazing service and care given, will visit once again"
"Love this product!!"
instead of what i wanted而不是我想要的
The obvious solution here is to simply unlist
your list:这里明显的解决方案是简单地unlist
列出您的列表:
list(text = unlist(mylist, use.names = FALSE))
#> $text
#> [1] "I love this product"
#> [2] "Amazing service and care given, will visit once again"
#> [3] "Love this product!!"
Created on 2022-07-31 by the reprex package (v2.0.1)由代表 package (v2.0.1) 于 2022 年 7 月 31 日创建
We could use bind_rows
:我们可以使用bind_rows
:
library(dplyr)
my_list %>%
bind_rows() %>%
mutate(Product = LETTERS[1:n()], .before=1)
Product text
<chr> <chr>
1 A I love this product
2 B Amazing service and care given, will visit once again
3 C Love this product!!
We can use我们可以用
c(l1,l2,l3) |> sapply(paste0) |>
unname() |> list(text = _)
$text
[1] "I love this product"
[2] "Amazing service and care given, will visit once again"
[3] "Love this product!!"
l1 <- list(text = "I love this product" )
l2 <- list(text = "Amazing service and care given, will visit once again" )
l3 <- list(text = "Love this product!!" )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.