【发布时间】:2020-02-14 07:34:19
【问题描述】:
我有以下数据框:
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
我怎样才能做到这一点?
【问题讨论】:
标签: r if-statement dplyr replace tibble