我有以下数据框:
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))