对 data.frame 或矩阵中的行求和 [英] Sum rows in data.frame or matrix

查看:51
本文介绍了对 data.frame 或矩阵中的行求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的数据框,行作为观察值,列作为遗传标记.我想创建一个新列,其中包含使用 R 的每个观察的选定列数的总和.

I have a very large dataframe with rows as observations and columns as genetic markers. I would like to create a new column that contains the sum of a select number of columns for each observation using R.

如果我有 200 列和 100 行,我想创建一个新列,该列有 100 行,其中包含 43 到 167 列的总和.这些列有 1 或 0.新列包含每一行的总和,我将能够对具有最多遗传标记的个体进行排序.

If I have 200 columns and 100 rows, I would like a to create a new column that has 100 rows with the sum of say columns 43 through 167. The columns have either 1 or 0. With the new column that contains the sum of each row, I will be able to sort the individuals who have the most genetic markers.

我觉得它很接近:

data$new=sum(data$[,43:167])

推荐答案

你可以使用 rowSums

rowSums(data) 应该给你你想要的.

这篇关于对 data.frame 或矩阵中的行求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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