r中rep(' each= ')的矢量化替代方法



我在rep()中得到一个警告消息,说each=参数未向量化。

是否有一种矢量化的替代rep(),可能是在tidyverse()或其他碱R替代?

n_study <- 5
n_per_study_rows <- c(3,5,3,3,2)
rep(1:n_study, each=n_per_study_rows)
Warning message: first element used of 'each' argument

使用times参数,而不是each参数:

n_study <- 5
n_per_study_rows <- c(3,5,3,3,2)
rep(1:n_study, times=n_per_study_rows)
#>  [1] 1 1 1 2 2 2 2 2 3 3 3 4 4 4 5 5

由reprex包(v2.0.0)于2018-10-25创建

我不清楚你是否真的想要repeachtimes版本。要使用each版本,您可以在sapply中使用rep:

unlist(sapply(n_per_study_rows, function(x) rep(1:n_study, each = x)))
#>  [1] 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 1 1
#> [35] 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 2 2 3 3 4 4

最新更新