【发布时间】:2019-12-04 16:46:33
【问题描述】:
是否可以生成一列序列
1 1 2 1 2 3 1 2 3 4 .... so on
使用dplyr
我尝试了lag() 函数,但没有明确的想法
library(dplyr)
test <- as.data.frame(c(1:1000))
names(test) <- 'a'
# View(test)
test <- test %>%
mutate(
c = # How to make this iterative to generate 1 2 2 3 3 3 4 4 4 4 .. so on
b = ifelse(a %% c < a , a , NA)
)
#tried to create 1 2 1 2 1 2 1 2 pattern
test <- test %>%
mutate(
c = 1,
c = ifelse(c <= lag(c),lag(c)+1,c)
)
预期输出是序列为1 1 2 1 2 3 1 2 3 4...so on的列
【问题讨论】: