另外一个选项:
x <- 1:48 x[-seq.int(0L, length(x), 4L)]
基准
x <- 1:48e6 library(microbenchmark) mbm <- microbenchmark( steven = x[-seq.int(0L, length(x), 4L)], venyao = x[x %% 4 != 0], venyao2 = as.vector(matrix(x, nrow=4)[-4, ]), pascal = x[as.logical((x) %% 4)], user20650 = as.integer(matrix(x, nrow=4)[-4, ]), times = 10 )
#Unit: milliseconds # expr min lq mean median uq max neval cld # steven 326.2159 350.6567 354.2743 357.3672 359.9924 368.3123 10 a # venyao 1388.9975 1395.8814 1417.3213 1400.1432 1455.2255 1470.7743 10 d # venyao2 613.9878 637.5377 639.1718 637.9342 640.1753 657.6627 10 b # pascal 1236.6055 1243.8149 1265.1976 1249.1046 1304.5699 1316.8247 10 c # user20650 587.8511 596.5614 610.4037 602.3607 619.1915 670.8756 10 b