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])
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.
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.
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.
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.
you can use rowSums
rowSums(data)
should give you what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With