Questions regarding rowsum, R function which gives column sums of a matrix or data frame, based on a grouping variable
Questions tagged [rowsum]
208 questions
4
votes
3 answers
r add columns in df1 with count of rows in df2 (conditional)
I have two data frames in R as below...I need to add new column(count_orders) in df1 which contains count of orders in df2 (or count of buyer in df2).
Please help.
> df1
buyer city
1 A xx
2 B yy
3 C zz
> df2
order buyer item
1 …

Ravi Kumar
- 161
- 1
- 6
4
votes
6 answers
Row and column sums in R
This is an example of how my data set (MergedData) looks like in R, where each of my participants (5 rows) obtained a score number in every test (7 columns). I would like to know the total score of all tests combined (all columns) but for each…

user3620588
- 41
- 1
- 1
- 2
4
votes
1 answer
rowsum for matrix over specified number of columns in R
I'm trying to get the sum of columns in a matrix in R for a certain row. However, I don't want the whole row to be summed but only a specified number of columns i.e. in this case all column above the diagonal. I have tried sum and rowSums function…

Triamus
- 2,415
- 5
- 27
- 37
4
votes
2 answers
Row-wise count of values that fulfill a condition
I want to generate a new variable which the number of times some columns satisfy a criterion (like ==, <, >). The function needs to handle NA.
Sample data with some missing values:
x <- seq(10, 20)
y <- seq(12, 22)
y[4] <- NA
z <- seq(14,…

SMM
- 193
- 1
- 3
- 12
3
votes
7 answers
Adding values of two rows and storing them in another column with repetition
I have a data frame like this
x1<- c(0,1,1,1,1,0)
df<-data.frame(x1)
I want to add another column that will take the sum of every two rows and store the value for the first two rows. This should look like this.
You can see here that the first two…

P Initiate
- 79
- 5
3
votes
4 answers
Sum every 3 columns of a dataframe to form new columns
I have a simple dataframe like the following:
ID, Type, a, b, c, d, e, f, etc.
ob1, 1, 1, 2, 3, 4, 5, 6, etc.
ob1, 2, 3, 4, 5, 6, 7, 1, etc.
I need to add the values of every 3 columns together, to produce new columns with the summed values.…

flâneur
- 633
- 2
- 8
3
votes
2 answers
rowSums if at least 1 group (set of columns) has greater than N counts in all replicates
I am working with RNA-seq data. I want to filter out genes where there are fewer than N counts in both replicates in at least one of my treatment groups.
My data is in a DESeq object, and the count data is structured like this, where each row is the…

FutureFellwalker
- 31
- 2
3
votes
2 answers
R Creating new columns using vector contains name of variables
I have a data and a vector contain name of variables and i want to create new variable contain rowsum of variables in my vector, and i want the name of new variable ( sum of variables in my vector) to be concatenation of names of variables
for…

Reda
- 449
- 1
- 4
- 17
3
votes
2 answers
subsetting data frame in R
I have a dataframe like this:
GENE a1 a2 a3 b1 b2 b3
G1 862 817 923 1096 997 946
G2 443 410 421 448 417 388
G3 396 348 372 428 351 361
G4 350 319 310 394 295 311
G5 350 332 341 412 303 316
G6 377 369 397 462 330 351
G7 362 348 399…

user3138373
- 519
- 1
- 6
- 18
3
votes
3 answers
Sequence Combination Matrix in R
I'm looking to create a matrix for 5 variables, such that each variable takes a value from seq(from = 0, to = 1, length.out = 500) and rowSums(data) = 1 .
In other words, I am wondering how to create a matrix that shows all the possible combinations…

Cezar Visan
- 33
- 5
3
votes
5 answers
simplifying R code using dplyr (or other) to rowSums while ignoring NA, unlss all is NA
I've initially solved my NA-issue helped by this questions. However, I would like to simplify my code. In the past, I've enjoyed the way dplyr has helped me simplify R code.
Below is a minimal working example illustrating my current solution and…

Eric Fail
- 8,191
- 8
- 72
- 128
3
votes
2 answers
Preserve xts index when using rowSums on xts
Is there a way to preserve the index of an xts object when passing rowSums a xts object?
Currently I recast the result as an xts object, but this doesn't seem to be as fast as it could be if rowSums was able to simply return what it was…

theGreatKatzul
- 437
- 1
- 5
- 16
3
votes
1 answer
R require row sums for occurrences of regex pattern that can occur multiple times in individual cells
I use r, and I'm looking to use regular expressions to calculate the row sums for the amount of occurrences of a string pattern that occurs across all columns in data frame containing epigenetic information. There are 40 columns, 15 of which may or…

Darren
- 277
- 4
- 17
3
votes
0 answers
R rowSums across specific columns of a matrix
I have a 1000 x 3 matrix of combinations of the integers from 1:10 (e.g. each column is an index ranging from 1 to 10 and I want to look at combinations of indices). I would like to get the row index of the combination that results in a partial row…

stats134711
- 624
- 1
- 5
- 15
3
votes
2 answers
Calculate rowMeans on a range of column (Variable number)
I want to calculate rowMeans of a range of column but I cannot give the hard-coded value for colnames (e.g c(C1,C3)) or range (e.g. C1:C3) as both names and range are variable. My df looks like:
> df
chr name age MGW.1 MGW.2 MGW.3 HEL.1 HEL.2…

Newbie
- 411
- 5
- 18