从init_list
或list_clustered_by_name
开始,我想到达desired_list
。我想知道是否有比我在这里开发的过程更好的过程:
也许是map_if之类的东西,检查长度,但我不能。
library(purrr)
cluster_by_name <- function (y) {
map(unique(names(y)), ~ paste(unique(y[which(names(y)==.)]), collapse=", ")) %>%
setNames(unique(names(y)))
}
flatten_sublist <- function(x, colnames) {
for (colname in colnames){
x <- append(x, unlist(x[[colname]]))
x[[colname]] <- NULL
}
x
}
vec <- c("A"=1,"B"=2, "A"=1, "B"=4)
vec2 <- c("A2"=1,"B2"=2, "A2"=3, "B2"=4)
init_list <- list(vec=vec, C=1, vec2=vec2)
init_list # starting point
list_clustered_by_name <- init_list %>%
map_if(function(x) length(x) > 1,
function (y) cluster_by_name(y)
)
list_clustered_by_name # possible starting point
# $vec
# $vec$A
# [1] "1"
# $vec$B
# [1] "2, 4"
# $C
# [1] 1
# $vec2
# $vec2$A2
# [1] "1, 3"
# $vec2$B2
# [1] "2, 4"
composed <- keep(list_clustered_by_name, ~(length(.) > 1), ~.)
single_features <- keep(list_clustered_by_name, ~(length(.) == 1), ~.)
composed_flattened <- flatten_sublist(composed, names(composed))
desired_list <- c(composed_flattened, single_features)
desired_list
# $A
# [1] "1"
# $B
# [1] "2, 4"
# $A2
# [1] "1, 3"
# $B2
# [1] "2, 4"
# $C
# [1] 1
2条答案
按热度按时间x6h2sr281#
这可能对你有用:
wko9yo5t2#
我不确定我的方法比你的好,但这是我想到的
输出: