【问题标题】:R :Looping through each 5 rows of data frame and imputing incremental value [closed]R:循环遍历每 5 行数据帧并估算增量值 [关闭]
【发布时间】:2018-12-10 17:33:27
【问题描述】:

我正在尝试为数据框的每 5 行估算增量值。我是 R 新手,不知道如何实现。

输入数据:

state Value 
  a    1
  b    2
  a    3
  c    4
  a    5
  e    6
  f    7
  w    8
  f    9
  s    10
  e    11
  r    12
  s    13
  s    14

期望的输出:

state Value Increment
  a    1     1
  b    2     1
  a    3     1
  c    4     1
  a    5     1
  e    6     2
  f    7     2
  w    8     2
  f    9     2
  s    10    2
  e    11    3
  r    12    3
  s    13    3
  s    14    3

【问题讨论】:

  • 我知道这是一个不清楚的问题,但也许到目前为止对所有 5 个答案都投反对票的人也可以解释问题和/或答案有什么问题。对于 OP:至少让您清楚地了解您正在尝试做的事情的逻辑,并包含您迄今为止尝试过的代码是有帮助的

标签: r loops data-munging


【解决方案1】:

这是您的数据:

df = read.table(text = 
                "state Value 
                     a     1
                     b     2
                     a     3
                     c     4
                     a     5
                     e     6
                     f     7
                     w     8
                     f     9
                     s     10
                     e     11
                     r     12
                     s     13
                     s     14", 
                header=T)

您现在可以使用rownames 来帮助您估算增量值。下面的代码行通过获取行索引,将它们除以5,然后获得ceiling(即最接近的更大整数),为您提供所需的输出。

df$Increment <- ceiling(as.numeric(rownames(df))/5)

这将为您提供预期的输出:

#    state Value Increment
# 1      a     1         1
# 2      b     2         1
# 3      a     3         1
# 4      c     4         1
# 5      a     5         1
# 6      e     6         2
# 7      f     7         2
# 8      w     8         2
# 9      f     9         2
# 10     s    10         2
# 11     e    11         3
# 12     r    12         3
# 13     s    13         3
# 14     s    14         3

希望对你有帮助。

【讨论】:

    【解决方案2】:

    这是一个dplyr 解决方案,它检查行号减一除以 5 的余数是否为 0。如果为 0,则将新列的值增加 1。

    dt = read.table(text = 
    "state Value 
    a    1
    b    2
    a    3
    c    4
    a    5
    e    6
    f    7
    w    8
    f    9
    s    10
    e    11
    r    12
    s    13
    s    14", header=T)
    
    library(dplyr)
    
    dt %>% mutate(Increment = cumsum((row_number()-1) %% 5 == 0))
    
    #    state Value Increment
    # 1      a     1         1
    # 2      b     2         1
    # 3      a     3         1
    # 4      c     4         1
    # 5      a     5         1
    # 6      e     6         2
    # 7      f     7         2
    # 8      w     8         2
    # 9      f     9         2
    # 10     s    10         2
    # 11     e    11         3
    # 12     r    12         3
    # 13     s    13         3
    # 14     s    14         3
    

    【讨论】:

      【解决方案3】:

      下面的函数会做你想做的。
      参数:

      1. DF - 输入数据帧;
      2. N- 增量中每个值的重复次数;
      3. newcol - 增量列的名称,默认为"Increment"

      只需将结果分配给新的 df。

      fun <- function(DF, N, newcol = "Increment"){
        n <- nrow(DF)
        f <- rep_len(c(1, rep(0, N - 1)), length.out = n)
        DF[[newcol]] <- cumsum(f)
        DF
      }
      
      fun(df1, N = 5)
      

      数据。

      set.seed(1234)    # Make the results reproducible
      n <- 14
      state <- sample(letters, n, TRUE)
      Value <- seq_len(n)
      df1 <- data.frame(state, Value)
      

      【讨论】:

        【解决方案4】:

        试试:

        rep(c(1:((nrow(df)/5)+1)),
            each=5,
            length.out=dim(df)[1])
        

        这给出了:

        > df$Increment<-rep(c(1:((nrow(df)/5)+1)),
        +     each=5,
        +     length.out=dim(df)[1])
        > df
           state Value Increment
        1      a     1         1
        2      b     2         1
        3      a     3         1
        4      c     4         1
        5      a     5         1
        6      e     6         2
        7      f     7         2
        8      w     8         2
        9      f     9         2
        10     s    10         2
        11     e    11         3
        12     r    12         3
        13     s    13         3
        14     s    14         3
        

        df 是:

        dt = read.table(text = 
        "state Value 
        a    1
        b    2
        a    3
        c    4
        a    5
        e    6
        f    7
        w    8
        f    9
        s    10
        e    11
        r    12
        s    13
        s    14", header=T)
        

        【讨论】:

          【解决方案5】:

          尝试:

          dt = read.table(text = 
                            "state Value 
          a    1
          b    2
          a    3
          c    4
          a    5
          e    6
          f    7
          w    8
          f    9
          s    10
          e    11
          r    12
          s    13
          s    14", header=T)
          
          dt$Increment<- unlist(lapply(1:ceiling(nrow(dt)/5), function(x) rep(x, 5) ))[1:nrow(dt)]
          dt
          

          【讨论】:

          • 没有给出想要的输出
          猜你喜欢
          • 1970-01-01
          • 2017-06-07
          • 2021-11-22
          • 2017-12-14
          • 1970-01-01
          • 1970-01-01
          • 2019-03-09
          • 2021-04-27
          相关资源
          最近更新 更多