Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何制作像1:n,1:n-1这样的序列。。。1:2, 1?_R_Vector_Sequence_Seq - Fatal编程技术网

R 如何制作像1:n,1:n-1这样的序列。。。1:2, 1?

R 如何制作像1:n,1:n-1这样的序列。。。1:2, 1?,r,vector,sequence,seq,R,Vector,Sequence,Seq,n是一个整数。我想要的顺序是: 1:n, 1:(n-1), 1:(n-2), ... , 1:3, 1:2, 1 编辑注: 在R中,1:n-1与1:(n-1)不同。小心。尽可能短于序列(n:1) 这个函数并不神秘 function (nvec) unlist(lapply(nvec, seq_len)) 因此,重新发明车轮。但是调用seq_len比调用其他选项更快,因为它是一个只有一个参数的基本函数(C函数) sequence2 <- function (nvec) unlist

n
是一个整数。我想要的顺序是:

1:n, 1:(n-1), 1:(n-2), ... , 1:3, 1:2, 1

编辑注:

在R中,
1:n-1
1:(n-1)
不同。小心。

尽可能短于
序列(n:1)


这个函数并不神秘

function (nvec) 
unlist(lapply(nvec, seq_len))
因此,重新发明车轮。但是调用
seq_len
比调用其他选项更快,因为它是一个只有一个参数的基本函数(C函数)

sequence2 <- function (nvec) unlist(lapply(nvec, seq.int, from = 1))
sequence3 <- function (nvec) unlist(lapply(nvec, function(x) 1:x))
sequence4 <- function (nvec) unlist(lapply(nvec, seq.default, from = 1))
sequence5 <- function (nvec) unlist(lapply(nvec, seq, from = 1))

library(microbenchmark)
microbenchmark(sequence(100:1), sequence2(100:1),
               sequence3(100:1), sequence4(100:1), sequence5(100:1))
#Unit: microseconds
#             expr     min        lq      mean    median        uq      max
#  sequence(100:1)  93.292  160.9325  205.5617  173.1995  200.0005 1157.201
# sequence2(100:1) 117.625  226.2120  308.4929  248.1055  280.8625 5477.710
# sequence3(100:1) 126.289  233.7875  365.6455  268.0495  301.8860 8808.911
# sequence4(100:1) 606.301 1195.4795 1463.3400 1237.5580 1344.3145 9986.619
# sequence5(100:1) 944.099 1864.3920 2063.3712 1942.2240 2119.3930 8581.593

## speed comparison
    seq     <  seq.default  <  function(x) 1:x  <  seq.int  <       seq_len
s3 generic       normal      light-weighted user  primitive   1-argument primitive
sequence2简称为
sequence(n:1)


这个函数并不神秘

function (nvec) 
unlist(lapply(nvec, seq_len))
因此,重新发明车轮。但是调用
seq_len
比调用其他选项更快,因为它是一个只有一个参数的基本函数(C函数)

sequence2 <- function (nvec) unlist(lapply(nvec, seq.int, from = 1))
sequence3 <- function (nvec) unlist(lapply(nvec, function(x) 1:x))
sequence4 <- function (nvec) unlist(lapply(nvec, seq.default, from = 1))
sequence5 <- function (nvec) unlist(lapply(nvec, seq, from = 1))

library(microbenchmark)
microbenchmark(sequence(100:1), sequence2(100:1),
               sequence3(100:1), sequence4(100:1), sequence5(100:1))
#Unit: microseconds
#             expr     min        lq      mean    median        uq      max
#  sequence(100:1)  93.292  160.9325  205.5617  173.1995  200.0005 1157.201
# sequence2(100:1) 117.625  226.2120  308.4929  248.1055  280.8625 5477.710
# sequence3(100:1) 126.289  233.7875  365.6455  268.0495  301.8860 8808.911
# sequence4(100:1) 606.301 1195.4795 1463.3400 1237.5580 1344.3145 9986.619
# sequence5(100:1) 944.099 1864.3920 2063.3712 1942.2240 2119.3930 8581.593

## speed comparison
    seq     <  seq.default  <  function(x) 1:x  <  seq.int  <       seq_len
s3 generic       normal      light-weighted user  primitive   1-argument primitive

sequence2可能李哲源'我们的解决方案不会得到改进。但出于好奇,一种非常普遍的R’ish方式可能是:

unlist(lapply(4:1, function(x) 1:x))
[1] 1 2 3 4 1 2 3 1 2 1

大概李哲源'我们的解决方案不会得到改进。但出于好奇,一种非常普遍的R’ish方式可能是:

unlist(lapply(4:1, function(x) 1:x))
[1] 1 2 3 4 1 2 3 1 2 1