Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.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 从另一个向量中获取向量_R - Fatal编程技术网

R 从另一个向量中获取向量

R 从另一个向量中获取向量,r,R,我有一个向量 x<-c(1,3,2,3,1,4,2,1,3,4,2,1,0,2,4,5,2,1,2) 谢谢您可以这样做: x[seq(from=1,to=10, by=2)] 您可以这样做: x[seq(from=1,to=10, by=2)] 你可以用 x[seq(1, 10, by=2)] 通过这种方式,使用seq(1,10,by=2)可以生成x元素的索引,然后使用[]操作符从x获取它们。您可以使用 x[seq(1, 10, by=2)] 使用seq(1,10

我有一个向量

x<-c(1,3,2,3,1,4,2,1,3,4,2,1,0,2,4,5,2,1,2)
谢谢

您可以这样做:

x[seq(from=1,to=10, by=2)]
您可以这样做:

x[seq(from=1,to=10, by=2)]
你可以用

x[seq(1, 10, by=2)]    
通过这种方式,使用
seq(1,10,by=2)
可以生成x元素的索引,然后使用
[]
操作符从
x
获取它们。

您可以使用

x[seq(1, 10, by=2)]    

使用
seq(1,10,by=2)
可以生成x元素的索引,然后使用
[]
操作符从
x
中获取它们。

这里有另一种方法可以做到这一点,尽管上面的建议更直接

x[1:10%%2 != 0][1:5]
@SimonO'Hanlon建议的类似方法(可能更好)

x[c(TRUE, FALSE)][1:5]

这里有另一种方法可以做到这一点,尽管上面的建议更直截了当

x[1:10%%2 != 0][1:5]
@SimonO'Hanlon建议的类似方法(可能更好)

x[c(TRUE, FALSE)][1:5]