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

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

问题描述

$ p

我正在绘制我的主成分分析的加载项。我有84行数据这样订购: x_1 y_1 x_2 ..... x_42 y_42



目前,我正在为x和y加载数据创建数据,如下所示:

  data.pc = princomp .matrix(data))

x.loadings< - data.frame(x = data.pc $ loadeds [c(1,3,5,7,9,11,13,15, 17,19,
21,23,25,27,29,31,33,35,37,39,41),1])$ ​​b
$ b 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])$ ​​b $ b

当然有一个更简单的方法来做到这一点?

解决方案

您可以随时使用seq生成序列:

  even_indexes< -seq(2,42,2)
odd_indexes< -seq(1,41,2)

然后,

  x.loadings<  -  data.frame(x = data.pc $ loadings [odd_indexes,1])$ ​​b $ b  


Is there a way in R to select many non-consecutive i.e. odd or even rows/columns?

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

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?

解决方案

You can always generate sequences with seq:

even_indexes<-seq(2,42,2)
odd_indexes<-seq(1,41,2)

Then,

  x.loadings <- data.frame(x=data.pc$loadings[odd_indexes,1])

这篇关于在R中为数据帧选择多个奇数或偶数列/行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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