【问题标题】:R repeat elements of data frameR重复数据框的元素
【发布时间】:2012-01-04 12:35:46
【问题描述】:

我已经搜索了互联网,但我无法找到解决问题的方法。 我有一个数字和字符的数据框:

mydf <- data.frame(col1=c(1, 2, 3, 4), 
                   col2 = c(5, 6, 7, 8), 
                   col3 = c("a", "b", "c", "d"), stringsAsFactors  = FALSE)

mydf:

col1 col2 col3
  1    5   a
  2    6   b
  3    7   c
  4    8   d

我想重复一下

col1 col2 col3
  1   5    a
  1   5    a
  1   5    a
  2   6    b
  2   6    b
  2   6    b
  3   7    c
  3   7    c
  3   7    c
  4   8    d
  4   8    d
  4   8    d

使用apply(mydf, 2, function(x) rep(x, each = 3)) 将给出正确的重复,但不会像我希望的那样将 col1、col2 和 col3 的类分别保存为数字、数字和字符。这是一个构造的例子,在我的数据框中设置每一列的类有点繁琐。

有没有办法在保留课程的同时进行重复?

【问题讨论】:

    标签: r dataframe repeat


    【解决方案1】:

    这比你想象的还要容易。

    index <- rep(seq_len(nrow(mydf)), each = 3)
    mydf[index, ]
    

    这也避免了来自apply 的隐式循环。

    【讨论】:

    • 听起来是时候重写zexpand :-)
    • 感谢大家的帮助和cmets!
    【解决方案2】:

    这是一次不幸且出乎意料的职业转换(无论如何,我也是如此)。这是一个简单的解决方法,它利用 data.frame 只是一个特殊列表这一事实。

    data.frame(lapply(mydf, function(x) rep(x, each = 3)))
    

    (有人知道为什么提问者观察到的行为不应该被报告为错误吗?)

    【讨论】:

      【解决方案3】:

      只是另一种解决方案:

      mydf3 <- do.call(rbind, rep(list(mydf), 3))
      

      【讨论】:

        【解决方案4】:

        看看raster 包中的aggregatedisaggregate。或者,在下面使用我的修改版本zexpand

        # zexpand: analogous to disaggregate
        
        zexpand<-function(inarray, fact=2, interp=FALSE,  ...)  {
        # do same analysis of fact to allow one or two values, fact >=1 required, etc.
        fact<-as.integer(round(fact))
        switch(as.character(length(fact)),
                    '1' = xfact<-yfact<-fact,
                    '2'= {xfact<-fact[1]; yfact<-fact[2]},
                    {xfact<-fact[1]; yfact<-fact[2];warning(' fact is too long. First two values used.')})
        if (xfact < 1) { stop('fact[1] must be > 0') } 
        if (yfact < 1) { stop('fact[2] must be > 0') }
        
        bigtmp <- matrix(rep(t(inarray), each=xfact), nrow(inarray), ncol(inarray)*xfact, byr=T)  #does column expansion
        bigx <- t(matrix(rep((bigtmp),each=yfact),ncol(bigtmp),nrow(bigtmp)*yfact,byr=T))
        # the interpolation would go here. Or use interp.loess on output (won't
        # handle complex data). Also, look at fields::Tps which probably does
        # a much better job anyway.  Just do separately on Re and Im data
        return(invisible(bigx))
        }
        

        【讨论】:

          【解决方案5】:

          我真的很喜欢 Richie Cotton 的回答。

          但您也可以简单地使用 rbind 并重新排序。

          res <-rbind(mydf,mydf,mydf)
          res[order(res[,1],res[,2],res[,3]),]
          

          【讨论】:

            【解决方案6】:

            mefa 带有一个很好的rep 包装器,适用于data.frame。这将在一行中匹配您的示例:

            mefa:::rep.data.frame(mydf, each=3)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-04-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-01-07
              • 2020-11-07
              • 2019-09-20
              • 2020-07-18
              相关资源
              最近更新 更多