gpt4 book ai didi

r - 按重复名称合并列表列表中的内容

转载 作者:行者123 更新时间:2023-12-01 11:55:54 25 4
gpt4 key购买 nike

给定一个列表列表,是否有一种优雅的方法将原始列表转换为处理后的列表?我使用的是简单的值,如 1、2、3,但值可以是数据框或其他任何东西。目标不是删除每个唯一命名的内容,只是通过合并内容来删除重复名称。

original = structure(list(name1 = structure(list(one = 1, two = 2, three = 3), .Names = c("one", 
"two", "three")), name2 = structure(list(a = 9), .Names = "a"),
name1 = structure(list(four = 4, five = 5, six = 6), .Names = c("four",
"five", "six")), name2 = structure(list(b = 8), .Names = "b")), .Names = c("name1",
"name2", "name1", "name2"))

treated = structure(list(name1 = structure(list(one = 1, two = 2, three = 3,
four = 4, five = 5, six = 6), .Names = c("one", "two", "three",
"four", "five", "six")), name2 = structure(list(a = 9, b = 8), .Names = c("a",
"b"))), .Names = c("name1", "name2"))

最佳答案

这是一个使用 plyr 的解决方案。

require(plyr)
lnames = names(original)
ulnames = unique(lnames)
treated = plyr::llply(ulnames, function(x) original[lnames == x])
treated = llply(treated, unlist, recursive = F)
names(treated) = ulnames

关于r - 按重复名称合并列表列表中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7349608/

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