Questions tagged [rowsum]

Questions regarding rowsum, R function which gives column sums of a matrix or data frame, based on a grouping variable

208 questions
105
votes
7 answers

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…
user483502
  • 1,051
  • 2
  • 7
  • 3
23
votes
5 answers

Omit inf from row sum in R

So I am trying to sum the rows of a matrix, and there are inf's within it. How do I sum the row, omitting the inf's?
Lcat91
  • 804
  • 3
  • 10
  • 14
17
votes
4 answers

Efficiently compute the row sums of a 3d array in R

Consider the array a: > a <- array(c(1:9, 1:9), c(3,3,2)) > a , , 1 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 , , 2 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 How do we…
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
14
votes
1 answer

R Programming - Sum Elements of Rows with Common Values

Hello and thank you in advance for your assistance, (PLEASE Note Comments section for additional insight: i.e. the cost column in the example below was added to this question; Simon, provides a great answer, but the cost column itself is not…
Jonathan Charlton
  • 1,975
  • 6
  • 23
  • 30
10
votes
2 answers

Sum all columns whose names start with a pattern, by group

I'm fairly new to R and I'm trying to sum columns by groups based on their names. I have a data frame like this one: DT <-…
Astarte
  • 163
  • 1
  • 1
  • 7
7
votes
4 answers

R sum of rows for different group of columns that start with similar string

I'm quite new to R and this is the first time I dare to ask a question here. I'm working with a dataset with likert scales and I want to row sum over different group of columns which share the first strings in their name. Below I constructed a data…
csmontt
  • 614
  • 8
  • 15
7
votes
2 answers

Summing values of 2D array on indices

I need to extend this question, which sums values of an array based on indices from a second array. Let A be the result array, B be the index array, and C the array to be summed over. Then A[i] = sum over C such that index(B) == i. Instead, my…
Kevin
  • 447
  • 4
  • 13
6
votes
4 answers

How do I compute the number of occurrences of a particular value in a row in R

I am having quite a tricky problem, which i just cannot seem to solve. I have a large dataset (23277 rows, 151 columns). Each column has values from 0:100 (inclusive) representing probabilities assigned for events in the world. As part of…
richiemorrisroe
  • 9,307
  • 3
  • 22
  • 20
5
votes
6 answers

Deal with missing data in row-wise manipulation

ID <- 1:6 math <- c("YES","NO","YES","NO",NA,NA) history <- c(NA,NA,"NO","NO","YES",NA) dt <- data.frame(ID, math, history) ID math history 1 1 YES 2 2 NO 3 3 YES NO 4 4 NO NO 5 5 YES 6 6
yoo
  • 491
  • 3
  • 10
5
votes
4 answers

Conditional rolling sum across columns

I have a data frame of values across successive years (columns) for unique individuals (rows). A dummy data example is provided here: dt = structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), `2015` = c(0, 0.8219178, 0, 0.1369863, 0, 1.369863,…
jmogil
  • 143
  • 8
5
votes
3 answers

Equivalent of rowsum function for Matrix-class (dgCMatrix)

For the base R matrix class we have the rowsum function, which is very fast for computing column sums across groups of rows. Is there an equivalent function or approach implemented in the Matrix-package? I'm particularly interested in a fast…
Misconstruction
  • 1,839
  • 4
  • 17
  • 23
5
votes
7 answers

How to get rowSums for selected columns in R

I am a newbie to R and seek help to calculate sums of selected column for each row. My simple data frame is as below. data = data.frame(location = c("a","b","c","d"), v1 = c(3,4,3,3), v2 = c(4,56,3,88), v3 =c(7,6,2,9), v4=c(7,6,1,9), …
sriya
  • 179
  • 1
  • 2
  • 7
4
votes
2 answers

Why does my R mutate across with rowSums not work (Error: Problem with `mutate()` input `..2`. x 'x' must be numeric ℹ Input `..2` is `rowSums(.)`.)?

I'm trying to learn how to use the across() function in R, and I want to do a simple rowSums() with it. However, I keep getting this error: Error: Problem with mutate() input ..2. x 'x' must be numeric ℹ Input ..2 is rowSums(., na.rm = TRUE). Yet,…
J.Sabree
  • 2,280
  • 19
  • 48
4
votes
5 answers

Merge two columns maintaning missing values

I am trying to add two columns. My dataframe is like this one: data <- data.frame(a = c(0,1,NA,0,NA,NA), x = c(NA,NA,NA,NA,1,0), t = c(NA,2,NA,NA,2,0)) I want to add some of the columns like this: yep <-…
Eric González
  • 465
  • 2
  • 10
4
votes
2 answers

Calculating Sum Column and ignoring Na

I am trying to create a Total sum column that adds up the values of the previous columns. However I am having difficulty if there is an NA. If there is an NA in the row, my script will not calculate the sum. How do I edit the following script to…
KKhosra
  • 163
  • 1
  • 4
  • 9
1
2 3
13 14