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
1
vote
3 answers
Create multiple sums
Ciao,
Here is a replicate able example.
df <- data.frame("STUDENT"=c(1,2,3,4,5),
"TEST1A"=c(NA,5,5,6,7),
"TEST2A"=c(NA,8,4,6,9),
"TEST3A"=c(NA,10,5,4,6),
…

bvowe
- 3,004
- 3
- 16
- 33
1
vote
1 answer
R: rowsum function changes order of groups after aggregation
I've got this data frame which has duplicates (same ID but different numbers):
ID X1 X2 X3 X4 X5
45 1 0 0 1 0
45 0 1 0 0 1
15 1 0 1 0 0
7 1 0 1 1 0
7 0 1 0 0 0
I want to sum the vectors that have the same ID so I've used…

pw225
- 51
- 6
1
vote
1 answer
Sum values of dataframe recodifying certain values of original variables on the fly
Let's say that I have the following dataframe:
df <- data.frame(a=c(1,2,3,3,1), b=(c(1,9,1,2,3)),
c=c(1,2,3,3,9), d=(c(1,2,3,9,1)))
I would like to sum the values of a + b + c + d, but every time that any of these variables has a…

Yatrosin
- 341
- 2
- 15
1
vote
2 answers
R: sum row based on several conditions
I am working on my thesis with little knowledge of r, so the answer this question may be pretty obvious.
I have the a dataset looking like…

Julia B
- 13
- 2
1
vote
3 answers
Rowsums conditional on column name in a loop
This is a follow-up question from this one: Rowsums conditional on column name
My data frame is called wiod and looks like this:
VAR1 VAR2 AUS1 ... AUS56 BEL1 ... BEL56 NLD1 ... NLD56
A D 23 ... 99 0 ... 444 123 ... 675
B D 55…

Laubsauger
- 69
- 2
- 9
1
vote
1 answer
Select subset of columns which minimise a criterion in R
I have a sparse binary data.frame which looks like this
set.seed(123)
dat <- as.data.frame(matrix(rep(round(runif(40,0,0.9),0),5),ncol = 20))
# > dat
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
# 1 0 0 1 1 0 …

hellter
- 944
- 14
- 31
1
vote
1 answer
Sum columns per row postgresql
I have table like this:
CREATE TABLE public."Payments"
(
user_id integer,
"1 month later" numeric(19,4),
"2 months later" numeric(19,4),
"3 months later" numeric(19,4),
"4 months later" numeric(19,4),
"5 months later" numeric(19,4),
…

Фанки Джаз
- 69
- 2
- 9
1
vote
1 answer
R - sum vectors matching names
I need to sum columns of a table that have a names starting with a particular string.
An example table might be:
tbl<-data.frame(num1=c(3,2,9), num2=c(3,2,9),n3=c(3,2,9),char1=c('a', 'b', 'c'))
I get the list of columns (in this example I wrote…

GabyLP
- 3,649
- 7
- 45
- 66
1
vote
1 answer
Efficient way of row/column sum of a IndexedRowmatrix in Apache Spark
I have a matrix in a CoordinateMatrix format in Scala. The Matrix is sparse and the entires look like (upon coo_matrix.entries.collect),
Array[org.apache.spark.mllib.linalg.distributed.MatrixEntry] = Array(
MatrixEntry(0,0,-1.0),…

Kent Carlevi
- 133
- 1
- 11
1
vote
1 answer
How do I divide rows by the row sum and rows that meet a certain condition (row Sum >100)in matrix?r
I have a matrix, and I am attempting to add a column at the end with the row sums, and then dividing the rows by the row sums, conditional on the row sum being greater than 100. This is what I have so far:
row.sums <- rowSums(a)
a <- cbind(a,…

Learning_R
- 609
- 1
- 6
- 11
1
vote
1 answer
R: Row sums for 1 or more columns
This is frustrating as it feels like it should be so simple but it's sucking up hours.
I want to get the row sums for a list of columns in a data frame, but that list - which is set by the user - may very well be length 1 i.e. one column…

dez93_2000
- 1,730
- 2
- 23
- 34
1
vote
1 answer
Convert a matrix of 1s and 0s to a row-sum count matrix
I would like to transform a matrix of 0s and 1s into a corresponding matrix that gives the cumulative row sum for non-zero entries. Example input and output is given below:
set.seed(404)
input <- matrix(rbinom(10 * 5, 1, 0.5), ncol = 5, nrow =…

jonnie
- 745
- 1
- 13
- 22
1
vote
3 answers
Summing rows of rolling columns
I need to apply a rolling function that sums the rows of every two columns, so the rows of columns 1&2 will be summed, 3&4, etc.
m<-matrix(c(1,2,3,4,5,3,4,5,6,2,4,6,6,7,3,2,4,4,5,7),nrow=2,byrow=T)
I have looked at many functions including apply,…

Nick Fisch
- 24
- 7
1
vote
1 answer
How to skip some row entries while adding rows of a data frame
I have a data frame of p-values. Other than p-values it has some zero entries also.
data
V1 V3 V4 V5 V6 V7
1 ADAM32 0.001 0 0 0 0
2 …

Agaz Wani
- 5,514
- 8
- 42
- 62
1
vote
2 answers
get pairwise sums of multiple columns in dataframe
I have a dataframe that looks like this:
x<-data.frame(a=6, b=5:1, c=7, d=10:6)
> x
a b c d
1 6 5 7 10
2 6 4 7 9
3 6 3 7 8
4 6 2 7 7
5 6 1 7 6
I am trying to get the sums of columns a & b and c&d in another data frame that should look…

Tavi
- 2,668
- 11
- 27
- 41