创建一个范围内有间隙的序列
Creating a sequence with a gap within a range
我正在尝试创建一个范围内的数字序列,跳过每 4 个数字。我知道它可能以某种方式涉及 seq
或 seq_len
,但我似乎无法弄清楚。
顺序应该是这样的:
c(1,2,3,5,6,7,9,10,11...48)
x <- 1:48
x[x%%4!=0]
# [1] 1 2 3 5 6 7 9 10 11 13 14 15 17 18 19 21 22 23 25 26 27 29 30 31 33 34 35
# [28] 37 38 39 41 42 43 45 46 47
更新:
我发现了一种更快的方法。
x <- 1:48e6
mbm <- microbenchmark(
steven = x[-seq.int(0L, length(x), 4L)],
venyao1 = x[x %% 4 != 0],
venyao2 = as.vector(matrix(x, nrow=4)[-4, ]),
pascal = x[as.logical((x) %% 4)],
times = 10
)
mbm
# Unit: milliseconds
# expr min lq mean median uq max neval
# steven 1519.4170 1524.0044 1553.3609 1554.0616 1554.7951 1639.989 10
# venyao1 3323.2142 3327.3154 3382.2387 3369.9388 3426.3029 3502.881 10
# venyao2 919.8674 921.3917 932.3078 923.1563 923.6811 1007.784 10
# pascal 3207.6932 3211.9832 3245.4686 3213.5090 3299.4639 3356.368 10
另一个选项:
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
我正在尝试创建一个范围内的数字序列,跳过每 4 个数字。我知道它可能以某种方式涉及 seq
或 seq_len
,但我似乎无法弄清楚。
顺序应该是这样的:
c(1,2,3,5,6,7,9,10,11...48)
x <- 1:48
x[x%%4!=0]
# [1] 1 2 3 5 6 7 9 10 11 13 14 15 17 18 19 21 22 23 25 26 27 29 30 31 33 34 35
# [28] 37 38 39 41 42 43 45 46 47
更新: 我发现了一种更快的方法。
x <- 1:48e6
mbm <- microbenchmark(
steven = x[-seq.int(0L, length(x), 4L)],
venyao1 = x[x %% 4 != 0],
venyao2 = as.vector(matrix(x, nrow=4)[-4, ]),
pascal = x[as.logical((x) %% 4)],
times = 10
)
mbm
# Unit: milliseconds
# expr min lq mean median uq max neval
# steven 1519.4170 1524.0044 1553.3609 1554.0616 1554.7951 1639.989 10
# venyao1 3323.2142 3327.3154 3382.2387 3369.9388 3426.3029 3502.881 10
# venyao2 919.8674 921.3917 932.3078 923.1563 923.6811 1007.784 10
# pascal 3207.6932 3211.9832 3245.4686 3213.5090 3299.4639 3356.368 10
另一个选项:
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