gpt4 book ai didi

r - 将所有相似的列表组合成一个列表列表

转载 作者:行者123 更新时间:2023-12-02 02:20:02 25 4
gpt4 key购买 nike

我有一个具有以下模式的列表列表:

[[1]]
[[1]]$Jobs
[1] "Principal Data Scientist" "Principal Data Scientist"
[3] "Principal Data Scientist" "Principal Data Scientist"
[5] "Data Science Analyst" "Data Science Analyst"

[[1]]$Company
[1] "IBM" "Lightpath" "Huawei Technologies"
[4] "Dealertrack" "J.P. Morgan" "Noviya Systems"

[[2]]
[[2]]$Jobs
[1] "Data Science Analyst" "Data Science Analyst"
[3] "Data Science Analyst" "Data Science Analyst"
[5] "Data Science Engineer" "Data Science Engineer"

[[2]]$Company
[1] "Petro IT" "Analytic Partners" "Affinity Health Plan"
[4] "IBM" "Commvault" "J. Crew"

[[3]]
[[3]]$Jobs
[1] "Data Science Manager" "Data Science Manager"
[3] "Data Science Manager" "Data Science Manager"
[5] "Staff Data Scientist" "Staff Data Scientist"

[[3]]$Company
[1] "ASDA group" "RTW Retailwinds" "BASF"
[4] "Futures Action Network" "RCI Exchanges" "Sizmek"

现在,我想要一个输出,其中所有相似的列表都被分组为一个,如下所示:

$Jobs
[1] "Principal Data Scientist" "Principal Data Scientist"
[3] "Principal Data Scientist" "Principal Data Scientist"
[5] "Data Science Analyst" "Data Science Analyst"
[7] "Data Science Analyst" "Data Science Analyst"
[9] "Data Science Analyst" "Data Science Analyst"
[11] "Data Science Engineer" "Data Science Engineer"
[13] "Data Science Manager" "Data Science Manager"
[15] "Data Science Manager" "Data Science Manager"
[17] "Staff Data Scientist" "Staff Data Scientist"

$Company
[1] "IBM" "Lightpath" "Huawei Technologies"
[4] "Dealertrack" "J.P. Morgan" "Noviya Systems"
[7] "Petro IT" "Analytic Partners" "Affinity Health Plan"
[10] "IBM" "Commvault" "J. Crew"
[13] "ASDA group" "RTW Retailwinds" "BASF"
[16] "Futures Action Network" "RCI Exchanges" "Sizmek"

如有任何帮助,我们将不胜感激。提前致谢!!!

最佳答案

这是一个嵌套列表。因此,我们遍历 liststack 到一个两列的 data.frame,rbind list 元素和split 基于 'ind' 列

d1 <- do.call(rbind, lapply(lst1, function(x) stack(x)))
with(d1, split(values, ind))

或者另一种选择是展平 listsplit

lst2 <- do.call(c, lst1)
split(unlist(lst2, use.names = FALSE), rep(names(lst2), lengths(lst2)))

或者也可以直接unlist,然后通过unlist向量的names的子串split

split(unlist(lst1, use.names = FALSE), sub("\\d+$", "", names(unlist(lst1))))

或者可以转置unlist

library(purrr)
transpose(lst1) %>%
map(unlist)

数据

lst1 <- list(list(Jobs = c("Principal Data Scientist", "Principal Data Scientist", 
"Data Science Analyst"), Company = c("IBM", "Lightpath")), list(
Jobs = c("Principal Data Scientist", "Principal Data Scientist",
"Data Science Analyst"), Company = c("IBM", "Lightpath")))

关于r - 将所有相似的列表组合成一个列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66572339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com