简体   繁体   English

R - 折叠到向量相同的列表成员

[英]R - Collapse into vector same member of a list

I have a list with same structure for every member as the following 我有一个列表,每个成员具有相同的结构,如下所示

config <- NULL

config[["secA"]] <- NULL
config[["secA"]]$VAL <- 0
config[["secA"]]$ARR <- c(1,2,3,4,5)
config[["secA"]]$DF  <- data.frame(matrix(c(1,5,3,8),2,2))

config[["secB"]] <- NULL
config[["secB"]]$VAL <- 1
config[["secB"]]$ARR <- c(1,3,2,4,9)
config[["secB"]]$DF  <- data.frame(matrix(c(2,6,1,9),2,2))

config[["secC"]] <- NULL
config[["secC"]]$VAL <- 5
config[["secC"]]$ARR <- c(4,2,1,5,8)
config[["secC"]]$DF  <- data.frame(matrix(c(4,2,1,7),2,2))

and I need to obtain 3 vectors VAL , ARR and DF , each with the concatenated elements of the corresponding member. 我需要获得3个向量VALARRDF ,每个向量都包含相应成员的连接元素。 such as

# VAL: 0,1,5
# ARR: 1,2,3,4,5,1,3,2,4,9,4,2,1,5,8
# DF:  1,5,3,8,2,6,1,9,4,2,1,7

Looking at similar situations, I have the feeling I need to use a combination of do.call and cbind or lapply but I have no clue. 看看类似的情况,我觉得我需要使用do.callcbindlapply的组合,但我不知道。 any suggestions? 有什么建议么?

config <- NULL

config[["secA"]] <- NULL
config[["secA"]]$VAL <- 0
config[["secA"]]$ARR <- c(1,2,3,4,5)
config[["secA"]]$DF  <- data.frame(matrix(c(1,5,3,8),2,2))

config[["secB"]] <- NULL
config[["secB"]]$VAL <- 1
config[["secB"]]$ARR <- c(1,3,2,4,9)
config[["secB"]]$DF  <- data.frame(matrix(c(2,6,1,9),2,2))

config[["secC"]] <- NULL
config[["secC"]]$VAL <- 5
config[["secC"]]$ARR <- c(4,2,1,5,8)
config[["secC"]]$DF  <- data.frame(matrix(c(4,2,1,7),2,2))

sapply(names(config[[1]]), function(x)
  unname(unlist(sapply(config, `[`, x))), USE.NAMES = TRUE)

# $VAL
# [1] 0 1 5
# 
# $ARR
# [1] 1 2 3 4 5 1 3 2 4 9 4 2 1 5 8
# 
# $DF
# [1] 1 5 3 8 2 6 1 9 4 2 1 7

Or you can use this clist function 或者您可以使用此clist功能

Unfortunately there were no other answers. 不幸的是没有其他答案。

(l <- Reduce(clist, config))
# $VAL
# [1] 0 1 5
# 
# $ARR
# [1] 1 2 3 4 5 1 3 2 4 9 4 2 1 5 8
# 
# $DF
#   X1 X2 X1 X2 X1 X2
# 1  1  3  2  1  4  1
# 2  5  8  6  9  2  7

It merges data frames and matrices, so you need to unlist to get the vector you want 它合并了数据框和矩阵,因此您需要unlist才能获得所需的矢量

l$DF <- unname(unlist(l$DF))
l
# $VAL
# [1] 0 1 5
# 
# $ARR
# [1] 1 2 3 4 5 1 3 2 4 9 4 2 1 5 8
# 
# $DF
# [1] 1 5 3 8 2 6 1 9 4 2 1 7

Function 功能

clist <- function (x, y) {
  islist <- function(x) inherits(x, 'list')
  '%||%' <- function(a, b) if (!is.null(a)) a else b
  get_fun <- function(x, y)
    switch(class(x %||% y),
           matrix = cbind,
           data.frame = function(x, y)
             do.call('cbind.data.frame', Filter(Negate(is.null), list(x, y))),
           factor = function(...) unlist(list(...)), c)

  stopifnot(islist(x), islist(y))
  nn <- names(rapply(c(x, y), names, how = 'list'))
  if (is.null(nn) || any(!nzchar(nn)))
    stop('All non-NULL list elements should have unique names', domain = NA)

  nn <- unique(c(names(x), names(y)))
  z <- setNames(vector('list', length(nn)), nn)

  for (ii in nn)
    z[[ii]] <- if (islist(x[[ii]]) && islist(y[[ii]]))
      Recall(x[[ii]], y[[ii]]) else
        (get_fun(x[[ii]], y[[ii]]))(x[[ii]], y[[ii]])
  z
}

Another approach, with slightly less code. 另一种方法,代码略少。

un_config <- unlist(config)

un_configNAM <- names(un_config)

vecNAM <- c("VAL", "ARR", "DF")

for(n in vecNAM){
  assign(n, un_config[grepl(n, un_configNAM)])
}

This will return 3 vectors as the OP requested. 这将返回3个向量作为OP请求。 However, generally it is more advantageous to store results in a list as rawr suggests. 但是,通常将结果存储在列表中更为有利,正如rawr建议的那样。 You of course can adopt the above code so that results are stored within a list. 您当然可以采用上面的代码,以便将结果存储在列表中。

l <- rep(list(NA), length(vecNAM))

i = 1
for(n in vecNAM){
  l[[i]] <- un_config[grepl(n, un_configNAM)]
  i = i +1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM