[英]Combine lists into a single list in R
我有單獨的列表,例如:
1. $text = "I love this product"
2. $text = "Amazing service and care given, will visit once again"
3. $text = "Love this product!!"
我怎樣才能將所有列表合並到一個列表中,這樣:
$text = ["I love this product", "Amazing service and care given, will visit once again", "Love this product!!"]
我想保存在 dataframe 中,這樣
Product Reviews
A ["I love this product", "Amazing service and care given, will visit once again", "Love this product!!"]
B ["I ......."]
dput
的輸入:
list(list(text="I love this product"),
list(text="Amazing service and care given, will visit once again"),
list(text="Love this product!!"))
當我使用 cbind 時,它返回:
Reviews
"I love this product"
"Amazing service and care given, will visit once again"
"Love this product!!"
而不是我想要的
這里明顯的解決方案是簡單地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!!"
由代表 package (v2.0.1) 於 2022 年 7 月 31 日創建
我們可以使用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!!
我們可以用
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.