Rlist,合并同名元素(有些是列表)
我有一个列表,其中的元素是列表,我想组合那些具有相同名称的元素,而不会失去其他元素的结构。 换句话说,我想把这个列表2转换成列表1Rlist,合并同名元素(有些是列表),r,R,我有一个列表,其中的元素是列表,我想组合那些具有相同名称的元素,而不会失去其他元素的结构。 换句话说,我想把这个列表2转换成列表1 list1= list ("credit" = list("Conceptualization", "Software"), ".attrs" = list ("contrib-type"= "author")) list2 =list ("credit" = "Conceptualization","credit" ="Software", ".attrs" =
list1= list ("credit" = list("Conceptualization", "Software"), ".attrs" = list ("contrib-type"= "author"))
list2 =list ("credit" = "Conceptualization","credit" ="Software", ".attrs" = list ("contrib-type"= "author"))
我试图使用unlist和我在这里找到的答案,但它破坏了.attrs元素:
tapply(unlist(list2, use.names = FALSE, recursive=FALSE), rep(names(list2), lengths(list2)), FUN = list)
$.attrs
$.attrs[[1]]
[1] "author"
$credit
$credit[[1]]
[1] "Conceptualization"
$credit[[2]]
[1] "Software"
我想你只想要第1级,而不是列表。我使用了
dplyr::lst
“注意lst
生命周期是有问题的”,因为它支持dplyr
准旋转
sapply(unique(names(list2)), function(x) {
#browser()
if(sum(names(list2)==x)>=2 & !any(sapply(list2[names(list2)==x], function(l) is.list(l)))){
item <- list2[names(list2)==x]
names(item) <- NULL
dplyr::lst(!!x := item)
} else {
list2[x]
}
}, USE.NAMES = FALSE)
$credit
$credit[[1]]
[1] "Conceptualization"
$credit[[2]]
[1] "Software"
$.attrs
$.attrs$`contrib-type`
[1] "author"
sapply(唯一(名称(列表2)),函数(x){
#浏览器()
if(sum(name(list2)=x)>=2&!any(sapply(list2[name(list2)==x],函数(l)是.list(l))){
谢谢你,它能工作!(这是一个很好的例子,说明我离真正的开发者有多远)@JulienColomb谢谢你的好话,我们都去过那里,只是呆在附近。