Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum rows in data.frame or matrix

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.

If I have 200 columns and 100 rows, then 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.

I feel it is something close to:

data$new=sum(data$[,43:167]) 
like image 520
user483502 Avatar asked Oct 21 '10 21:10

user483502


People also ask

Why would you use a matrix instead of a DataFrame type?

Both represent 'rectangular' data types, meaning that they are used to store tabular data, with rows and columns. The main difference, as you'll see, is that matrices can only contain a single class of data, while data frames can consist of many different classes of data.

How do I sum a row in a matrix in R?

To calculate the sum of rows of an array in R, use the rowSums() function. Let's create an array and use the rowSums() function to calculate the sum of rows of the array. To create an array in R, use the array() function.

How do I sum an array of rows in R?

Compute the Sum of Rows of a Matrix or Array in R Programming – rowSums Function. rowSums() function in R Language is used to compute the sum of rows of a matrix or an array. dims: Integer: Dimensions are regarded as 'rows' to sum over.

How do you find the row sum and column sum of a matrix in R?

To find the sum of row, columns, and total in a matrix can be simply done by using the functions rowSums, colSums, and sum respectively.


1 Answers

you can use rowSums

rowSums(data) should give you what you want.

like image 124
Greg Avatar answered Oct 02 '22 16:10

Greg