设计代码以生成特定模式

Ani*_*mar 1 r vector dplyr

是否可以生成一列序列

1 1 2 1 2 3 1 2 3 4 .... so on 
Run Code Online (Sandbox Code Playgroud)

使用 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)
  )
Run Code Online (Sandbox Code Playgroud)

预期输出是具有序列的列 1 1 2 1 2 3 1 2 3 4...so on

mar*_*kus 5

尝试

sequence(1:5)
# [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)

sequence是一个包装lapplyseq_len

function (nvec) unlist(lapply(nvec, seq_len))
Run Code Online (Sandbox Code Playgroud)