我有以下数据框:
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
我该如何实现?
我有以下数据框:library(dplyr)library(tibble)df
基础R解决方案
dplyr
解决方案。
这里是dplyr
方法,