r - Select cases in a data frame -
i want select range of cases in data frame, every n column. illustrate problem reproducible example:
set.seed(100) data <- data.frame(replicate(18,sample(0:100,18,rep=true)))
from data, want select data[1:6, 1]
, data[7:12, 7]
, data[13:19, 13]
, , forth. obviously, i'm working bigger dataset (>10000 rows , columns), why prefer automated way this.
have tried define sequence beforehand (seq()
) couldn't quite figure out how apply problem.
help!
matrix
indexing might handy here:
sel <- cbind(sequence(nrow(data)),rep(seq(1,ncol(data),6),each=6)) sel # row col # [,1] [,2] # [1,] 1 1 # ... # [6,] 6 1 # [7,] 7 7 # ... #[12,] 12 7 #[13,] 13 13 # ...
then:
data[sel] # [1] 31 26 55 5 47 48 97 3 92 73 20 84 37 30 55 37 85 62
Comments
Post a Comment