我有以下数据框:
library(dplyr)
library(tibble)
df <- tibble(
source = c("a", "b", "c", "d", "e"),
score = c(10, 5, NA, 3, NA ) )
df
看起来是这个样子:
# A tibble: 5 x 2
source score
<chr> <dbl>
1 a 10 . # current max value
2 b 5
3 c NA
4 d 3
5 e NA
我想做的是将分数列中的NA
替换为现有max + n
之后的值,其中n
的范围从1到df
的总行数。最终结果如下(手工编码):
source score
a 10
b 5
c 11 # obtained from 10 + 1
d 3
e 12 # obtained from 10 + 2
我如何实现这个目标?
seq(which(is.na(df$score)))
缩短为1:sum(is.na(df$score))
。 - s_baldur