简体   繁体   English

如何根据行号对r中的行进行分组

[英]How to group rows in r based on row number

I would like to make 2 groups based on their row numbers (the 1st group being rows 2 to 47, and the 2nd group being rows 48 to 92). 我想根据它们的行号将其分为两个组(第一个组是第2至47行,第二个组是第48至92行)。 The groups are my top and bottom performing samples and I would like to compare the groups' values in the 12 data columns (genes being tested). 这些组是我表现最好和最差的样本,我想在12个数据列(正在测试的基因)中比较这些组的值。 So, my ultimate goal is to divide the samples into their appropriate groups, and run statistical analyses the group's values for each of the genes tested. 因此,我的最终目标是将样本分为适当的组,并对每个测试的基因进行统计分析。 Here is a small section of my table: 这是我桌子的一小部分:

Sample    icaA   icaB   icaC   icaD
ST1       12     13     15     18
ST2       11     9      8      16
ST3       15     18     18     15
ST4       13     16     17     20

I don't know if I can use cbind to combine the groups. 我不知道我是否可以使用cbind组合组。 I think I've also seen others flip the rows and columns; 我想我也看到其他人翻转行和列。 I can do that if needed. 如果需要,我可以这样做。 I'm just a beginner with the software, so any suggestions would be great! 我只是该软件的初学者,所以任何建议都很棒!

To get the first group: 要获得第一组:

df1 <- df[2:47, ]

To get the second group: 要获得第二组:

df2 <- df[48:92, ]

Right? 对?

Then you can run stats on each column, for instance, like this: 然后,您可以在每个列上运行统计信息,例如:

apply(df1[-1], 2, mean)

...To get the mean for each column, in the first group. ...为了获得第一列中每一列的平均值。

Then for the mean of each column in the second group: 然后针对第二组中每列的平均值:

apply(df2[-1], 2, mean)

Then to bind each group into 1 dataframe (or matrix) again, then I recommend: 然后将每个组再次绑定到1个数据帧(或矩阵)中,那么我建议:

rbind(df1, df2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM