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
2 answers
Troubleshooting R's rowsum
I am troubleshooting the R's row sum function. I have the following vector called total:
1 3
1 45
.. ..
20 45
20 46
The vector has 20 different categories, and I would like to sum all the values for each category. My code is:
…

Johnathan
- 1,877
- 4
- 23
- 29
1
vote
1 answer
Sum selected rows in a character matrix: error 'x' must be numeric in R
I am new at R and this is really basic, but it is not working for me. I want to sum the rows from col 6:11 in this data.frame, for instance:
dput(tab.res)
tab.res <- structure(c("RIL", "RIL", "RIL", "RIL", "RIL", "RIL", "RIL", "RIL",
"RIL",…

TWest
- 765
- 2
- 6
- 27
0
votes
2 answers
R: Matrix with colSums and rowSum constrained by 2 vectors
Is there more elegant (less code) way of find a matrix OUT,
with colSums(OUT)<=a and rowSums(OUT)<=b,
given ORD = order of filling
sum(OUT) -> maximised
Sudoku-like problem, (numbers are not unique and filling order is given, so not really…

user1291918
- 3
- 2
0
votes
2 answers
How to compare values in two columns and if values are equal keep as is, but if values are different, sum by row
Using R, I would like to compare values in two columns, if the values are equal I would like to keep them as is, if the values are different, I would like to sum the values in the two columns.
This seems like a simple operation but I can't figure…

FishyFishies
- 301
- 3
- 14
0
votes
2 answers
Generate an m*n matrix with 1 and 0 (No identity)
I need to generate an m*n matrix with 1 and 0 where the sum of each row is 1 and each column is greater than or equal to 1.
I'm trying this code but I don't know how to limit my condition on the columns, since sometimes I get zero columns and I need…

user21559
- 7
- 2
0
votes
0 answers
rowSums adding new column to dataframe but not adding the values
I have a table that I'm trying to add multiple columns together in a new column. I've tried simple rowSums in different forms and it will create the new column but the values don't add.
col1
col2
col3
col4
10
25
15
25
99
42
20
35
I've…

Clay Pettit
- 1
- 1
0
votes
0 answers
Rowsums with a vector of (many) selected columns in r
I need to create a column ("rt") that sums up 50 selected columns in each row of a dataframe ("data").
I already have a vector named "rtvecnum" which contains the 50 columns' numbers needed to be summed.
the first row of the dataframe is irrelevent…

ChenG
- 19
- 3
0
votes
1 answer
Sum rows under condition of multiple columns in R
I wish to sum the rows (here in this case seats of parties in parliament) but only if the party voted yes (here indicated by 1 for yes and 0 for no).
Example data frame:
cabinet <- c("A", "B", "C")
seats1 <- c(20, 30, 40)
seats2 <- c(10, 15,…
0
votes
1 answer
How do I use the across function across all columns of a dataframe rather than specify certain columns?
I have the following script so far that successfully creates a new column in my dataframe and populates it with a sum of how many times the value "TRUE" appears for each row of the dataframe:
data_1 <- data_1 %>% mutate(True_Count =…

Brenda Thompson
- 327
- 2
- 9
0
votes
2 answers
R check equality of one column to rowSums of other columns
I have a dataframe like this:
x
y
x1
y1
x2
y2
x3
y3
1
0
1
0
0
0
0
0
0
0
3
0
0
0
0
0
2
0
0
0
0
0
2
0
1
0
0
0
1
0
0
0
I want to find rows that x=x1+x2+x3 and rows that y=y1+y2+y3.
Here is my code to check x=x1+x2+x3:
col_x =…

xiaochuan fang
- 104
- 9
0
votes
2 answers
how to compute row means iff the number of NA's is smaller than a given value
I have questionnaire data (rows=individuals, cols=scores on questions)and would like to compute a sumscore for individuals if they answered a given number of questions, otherwise the sumscore variable should be NA. The code below computes row sums,…

Gitta
- 15
- 2
0
votes
2 answers
Calculate row sums exclude the first n columns
I need to calculate row sums for a data frame except for the first 5 columns. The output will consist of these first 5 columns and the row sums.
I tried this:
df1$rowsums <- rowSums(df1[,-c(1:5)], na.rm= T)
But I get this error message:
Error in…

magwabat
- 11
- 4
0
votes
0 answers
Selecting specific rows and columns in r
I'm hoping to get some advice from the community about functions that require a selection of rows and columns. I have a very messy database (real-world data from a central database) and I need to sum subscales for a total score. To make matters more…

Laura
- 11
- 1
0
votes
2 answers
R mutate() with rowSums()
I want to take a dataframe of participant IDs and the languages they speak, then create a new column which sums all of the languages spoken by each participant. The columns are the ID, each language with 0 = "does not speak" and 1 = "does speak",…

talkingood
- 3
- 2
0
votes
1 answer
R sum an array along a specified dimension
I am looking for a way to sum an array along a specified dimension.
I was able to reproduce the example given in the MathWorks documentation for MATLAB. My R code refers to the following part of the MATLAB documentation:
A = ones(4,2,3);
S =…

Steffen Schwerdtfeger
- 109
- 8