R:循环遍历每 5 行数据框并输入增量值

R :Looping through each 5 rows of data frame and imputing incremental value

我正在尝试为数据框的每 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

尝试:

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

以下函数将执行您想要的操作。
参数:

  1. DF - 输入 data.frame;
  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)

这是一个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

这是您的数据:

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

希望对您有所帮助。

尝试:

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)