【问题标题】:Transform a splitted data.frame in a new data. frame在新数据中转换拆分的 data.frame。框架
【发布时间】:2013-02-15 16:43:27
【问题描述】:

我有一个如下所示的 data.frame:

   Element1     Element2        Value           Index   
         a         cf            0.14             1           
         a         ng            0.25             1           
         a         ck            0.12             1         
         a         rt            0.59             1      
         a         pl            0.05             1          
         b         gh            0.02             2          
         b         er            0.91             2
         b         jk            0.87             2
         c         qw            0.23             3
         c         po            0.15             3

我想要以下输出:

   Element_a1     Element_a2    Value_a       Element_b1   Element_b2  Value_b
         a         cf            0.14             b            gh       0.02      
         a         ng            0.25             b            er       0.91   
         a         ck            0.12             b            jk       0.87
         a         rt            0.59             NA           NA       NA
         a         pl            0.05             NA           NA       NA

等等……

我应用“split”函数根据“Index”列拆分初始 data.frame,但我无法根据需要在单个 data.frame 中转换拆分后的 data.frame(即 data.frames 列表),因为单个 data.frames 的长度不相等。我试图申请(来自 ply 包)

x = do.call(rbind.fill, spl)

与另一篇文章一样,但返回了与最初的一样的 data.frame。

【问题讨论】:

    标签: r


    【解决方案1】:

    这是一种方法:

    nRow <-  max(table(dat$Element1))          # maximum number of rows in a group
    spl2 <- by(dat, dat$Element1, FUN = function(x) {           
      if (nRow > nrow(x)) {                    # insufficient number of rows?
        subdat <- dat[seq_len(nRow - nrow(x)), ]  # create a data frame
        subdat[ , ] <- NA                      # fill it with NAs
        return(rbind(x, subdat))}       # bind it to the subset and return the result
      return(x)                                # return the subset as it is
    })
    result <- do.call(cbind, spl2)             # bind all subsets together
    

    【讨论】:

    • +1!我会这样做..也许你可以用 by 替换你的拆分 - lapply 类似 ..`` by(dat,dat$Element1,..`
    【解决方案2】:

    我会使用split 然后cbind 他们一起,后填充。我从combining two data frames of different lengths借用了cbindPad函数:

    cbindPad <- function(...){
      args <- list(...)
      n <- sapply(args,nrow)
      mx <- max(n)
      pad <- function(x, mx){
        if (nrow(x) < mx){
          nms <- colnames(x)
          padTemp <- matrix(NA,mx - nrow(x), ncol(x))
          colnames(padTemp) <- nms
          return(rbind(x,padTemp))
        }
        else{
          return(x)
        }
      }
      rs <- lapply(args,pad,mx)
      return(do.call(cbind,rs))
    }
    
    ## assume your data is in a data.frame called dat
    dat_split <- split(dat, dat$Element1)
    out <- do.call( cbindPad, dat_split )
    

    【讨论】:

    • 嗨 CauchyDistributedRV!非常感谢!它工作得很好。我永远无法编写如此复杂的函数。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-08-26
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多