为数据框选择多个奇数或偶数列/行 [英] Selecting multiple odd or even columns/rows for dataframe
本文介绍了为数据框选择多个奇数或偶数列/行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 R 中有没有办法选择许多非连续的,即奇数或偶数行/列?
Is there a way in R to select many non-consecutive i.e. odd or even rows/columns?
我正在为我的主成分分析绘制载荷图.我有 84 行这样排序的数据:x_1
y_1
x_2
..... x_42
y_42
I'm plotting the loadings for my Principal Components Analysis. I have 84 rows of data ordered like this: x_1
y_1
x_2
..... x_42
y_42
目前我正在为 x 和 y 载荷图创建数据框,如下所示:
And at the moment I am creating the dataframes for the x and y loadings figures like this:
data.pc = princomp(as.matrix(data))
x.loadings <- data.frame(x=data.pc$loadings[c(1, 3, 5, 7, 9, 11, 13 ,15, 17, 19,
21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41), 1])
yloadings <- data.frame(y=data.pc$loadings[c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42), 1])
当然有更简单的方法来做到这一点吗?
Surely there's an easier way to do this?
推荐答案
你总是可以用 seq 生成序列:
You can always generate sequences with seq:
even_indexes<-seq(2,42,2)
odd_indexes<-seq(1,41,2)
那么,
x.loadings <- data.frame(x=data.pc$loadings[odd_indexes,1])
这篇关于为数据框选择多个奇数或偶数列/行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文