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
3
votes
2 answers
Count occurrences of a string, by row, in a large data frame
I am trying to count a binary character outcome by row in a large data frame:
V1 V2 V3 V4 V5
Loss Loss Loss Loss Loss
Loss Loss Win Win Loss
Loss Loss Loss Loss Loss
Reprex:
df <-…

mike
- 123
- 2
- 4
2
votes
2 answers
Summing rows with a dynamic column range specified by another value in that row
I am working with a set of binary data for months of the year similar to the sample tibble below but with tens of thousands of rows. I am trying to add a column to the existing table that sums each row between the month specified by the column Month…

Brad
- 21
- 1
2
votes
3 answers
rowsums of specific columns filling a condition
I'm trying to sum specific columns of my data that fills some condition,
like the example underneath
library(dplyr)
library(readr)
set.seed(123)
data=data.frame(id=1:4,
v1=sample(c("a","b"),4,TRUE),
…

Wael
- 1,640
- 1
- 9
- 20
2
votes
2 answers
how to exclude single column from mutate max operation but retain all columns in dataframe
I am trying to get both the sum across rows and the max value in a row. I obviously do not want the rowsum column to be included in the max values, nor do i want the max values included in the row sum. I need a final dataset that has both of these…

sscoresby
- 67
- 5
2
votes
2 answers
Vector of R column names that are equal to a value
I want to sum across undefined amount of columns that start with the same string pattern. I would like for each column of my new data frame to have the column name of the string that was used in column name search. However, I am not sure, how to…

aerospace
- 35
- 4
2
votes
3 answers
r rowSums in case_when
Considering this is my dataset
df <-
structure(list(id = 1:6, w = c(NA, 11L, 12L, 13L, 14L, 15L),
x = c(20L, 21L, NA, 23L, 24L, 25L), y = c(30L, 31L, 32L,
NA, 34L, 35L), z = c(40L, NA, 42L, 43L, 44L, NA), Group = c("Group1",
…

Ahir Bhairav Orai
- 611
- 1
- 7
2
votes
1 answer
R: Summarize groups of rows in one data.frame with groups based on whether a date column's value falls in a time range in another data.frame
I am trying to find the rows sums for each column in data frame df_count (cars, buses,trucks) between the time frames given in each row in the data frame start_end
So for example, row 1 of start_end ranges from 2021-06-12 00:15:00 to 2021-06-12…

James
- 137
- 7
2
votes
3 answers
How to find the number of columns of an R matrix that contains values greater than or equal to a given number?
I have a matrix with 52 columns, and 5,000 rows. I want to find the number of columns that contain a value less than or equal to a value (for example, how many columns out of 52 contain a number less than or equal to 10)
I was trying rowSum but I…

Ashleigh Tindle
- 21
- 1
2
votes
1 answer
rowsum for multiple columns in r
I can take the sum of the target column by the levels in the categorical columns which are in catVariables. However, instead of doing this in a for loop I want to apply this to all categorical columns at once. For loop will make the code run for…

Mine
- 831
- 1
- 8
- 27
2
votes
5 answers
Pandas row sum for values > 0
I have a dataframe of the following format
ROW Value1 Value2 Value3 Value4
1 10 10 -5 -2
2 50 20 -10 -7
3 10 5 0 -1
I am looking to calculate for each row the sum of…

FlyingPickle
- 1,047
- 1
- 9
- 19
2
votes
1 answer
Counting the occurences of a string in dataframe row
I have a data frame (named as df) of 144 columns (trial numbers) containing the information about the trial success (Yes/No) per participant (the rows). A subset would look like this:
V1 V2 V3 V4 V5
Yes No Yes Yes …

e. erhan
- 61
- 6
2
votes
1 answer
In R, how to do rowsums for columns with negative and positive integers?
I'm trying to sum 4 columns, some of which have negative values.
I get an error message when I do so:
Error in .subset(x, j) : only 0's may be mixed with negative subscripts
Is there a way to overcome this and get the sum? I read what's been…

Pre
- 111
- 7
2
votes
1 answer
how to sort a matrix into a block matrix (zero blocks), preserving naming order on ties using row/col Sums?
I have an adjacency matrix:
myV = c(0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,1,1,0,0,0,0,0,0,0,
1,1,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,0,0,
…

mshaffer
- 959
- 1
- 9
- 19
2
votes
1 answer
use %in% in operator with select in R
I have a dataset that I want to calculate each participants participation rate for certain columns (number of non-NAs/total columns). The actual dataset has many columns that I want to ignore.
For this, let's imagine I only want to know the…

J.Sabree
- 2,280
- 19
- 48
2
votes
2 answers
R: get the column names where rowSum is equal to a value
I have a dataframe with multiple columns where its rowSums is either 1 either 0.9.
If one column is 0.5, than another one has to be 0.5. If a column is 0.3, then other two have to have the same value.
df <- data.frame(A = c(0, 0, 0.3, 0.5, 0, 0.3,…

Andrei Niță
- 517
- 1
- 3
- 14