我收到一條警告訊息rep(),指出each=引數未矢量化。
是否有rep()可能替代intidyverse()或其他基本 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
uj5u.com熱心網友回復:
使用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 )于 2021 年 10 月 25 日創建
uj5u.com熱心網友回復:
這不是來自你是否真的希望這個問題很清楚,我each和times版本rep。要使用該each版本,您可以使用repinside sapply:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/336411.html
上一篇:從for回圈創建串列
下一篇:遍歷for回圈時無法一一列印值
