R:从序列矢量中选择多个值

kse*_*sed -2 r vector sequence indices

在R中我试图弄清楚如何从预定义的序列矢量中选择多个值(例如indices = c(1:3, 4:6, 10:12, ...)).换句话说,如果我想要一个带有"indices"中第3,第5和第7个条目的新向量,我应该使用什么语法来获取只有那些序列完整的向量,例如c(10:12, ...)

Ale*_* A. 5

如果我理解正确,您需要第3,第5和第7个条目c(1:3, 4:6, 10:12, ...),这意味着您需要从向量中提取特定的索引.

当你做类似的事情时c(1:3, 4:6, ...),得到的矢量不是你想要的.相反,使用list(1:3, 4:6, ...).然后你可以这样做:

indices <- list(1:3, 4:6, 10:12, 14:16, 18:20)

x <- rnorm(100)

x[c(indices[[3]], indices[[5]])]
Run Code Online (Sandbox Code Playgroud)

这相当于:

x[c(10:12, 18:20)]
Run Code Online (Sandbox Code Playgroud)

这相当于:

x[c(10, 11, 12, 18, 19, 20)]
Run Code Online (Sandbox Code Playgroud)

如果我误解了你的问题,请告诉我.