Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.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
使用rep()和seq()创建向量_R_Seq_Rep - Fatal编程技术网

使用rep()和seq()创建向量

使用rep()和seq()创建向量,r,seq,rep,R,Seq,Rep,如何创建以下向量序列: 2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6 7 8 5 6 7 8 6 7 8 7 8 我尝试使用: 2:8+rep(0:6,each=6) 但结果是: 2 3 4 5 6 7 8 3 4 5 6 7 8 9 4 5 6 7 8 9 10 .... 12 13 14 请帮忙。谢谢。您可以这样做: library(purr) unlist(map(2:7, ~.x:8)) # [1] 2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6

如何创建以下向量序列:

2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6 7 8 5 6 7 8 6 7 8 7 8
我尝试使用:

2:8+rep(0:6,each=6)
但结果是:

2 3 4 5 6 7 8 3 4 5 6 7 8 9 4 5 6 7 8 9 10 .... 12 13 14
请帮忙。谢谢。

您可以这样做:

library(purr)
unlist(map(2:7, ~.x:8))

# [1] 2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6 7 8 5 6 7 8 6 7 8 7 8
在base R中有一个小函数:

funky_vec <- function(from,to){unlist(sapply(from:(to-1),`:`,to))}
funky_vec(2,8)
# [1] 2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6 7 8 5 6 7 8 6 7 8 7 8
funky_vec您可以这样做:

library(purr)
unlist(map(2:7, ~.x:8))

# [1] 2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6 7 8 5 6 7 8 6 7 8 7 8
在base R中有一个小函数:

funky_vec <- function(from,to){unlist(sapply(from:(to-1),`:`,to))}
funky_vec(2,8)
# [1] 2 3 4 5 6 7 8 3 4 5 6 7 8 4 5 6 7 8 5 6 7 8 6 7 8 7 8

funky_-vec这应该可以满足您的需求:

x = 2
VecSeq = c(x:8)

while (x < 7) {
    x = x + 1
    calc = c(x:8)
    VecSeq = c(VecSeq, calc)
}

VecSeq # Your desired vector
x=2
VecSeq=c(x:8)
而(x<7){
x=x+1
计算=c(x:8)
VecSeq=c(VecSeq,计算)
}
VecSeq#您想要的向量

这应该可以满足您的需求:

x = 2
VecSeq = c(x:8)

while (x < 7) {
    x = x + 1
    calc = c(x:8)
    VecSeq = c(VecSeq, calc)
}

VecSeq # Your desired vector
x=2
VecSeq=c(x:8)
而(x<7){
x=x+1
计算=c(x:8)
VecSeq=c(VecSeq,计算)
}
VecSeq#您想要的向量

unlist(lappy(2:7,函数(x){x:8}))也可以在没有附加包的情况下工作是的,我想先给出一个漂亮的答案,请参阅更新后的答案unlist(lappy(2:7,函数(x){x:8}))也可以在没有附加包的情况下工作是的,我想先给出漂亮的答案,请参阅更新后的答案
unlist(Map(“:”,2:7,8))
sapply(2:7,函数(i)seq(i,8))
类似地,
sapply(7:2,函数(x)seq(to=8,length.out=x))
@Sotos,这与
sapply(2:7,seq,8)
相同,但您仍然需要
取消列出
头(序列(7:1)+代表(1:7,7:1),-1)
取消列出
类似地,
sappy(7:2,函数(x)seq(to=8,length.out=x))
@Sotos,这与
sappy(2:7,seq,8)
相同,但仍然需要
取消列出
头部(序列(7:1)+代表(1:7,7:1),-1)