为数据帧选择多个奇数或偶数列/行 [英] Selecting multiple odd or even columns/rows for dataframe

查看:146
本文介绍了为数据帧选择多个奇数或偶数列/行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆