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
1
vote
5 answers

Create multiple columns with R dplyr mutate with across instead of with a loop?

I am trying to use R's dplyr package to create multiple new columns for each year in my dataset that is the sum of the columns corresponding to each year's end of quarter figures (Mar, Jun, Sep, Dec). The only way I have been able to figure out how…
StatsStudent
  • 1,384
  • 2
  • 10
  • 28
1
vote
2 answers

Keep the rows where the rowsum of at least one of the predefined subsets of the columns is greater than a threshold

I have a dataframe like this: df <- data.frame( sample1 = c(0, 1, 2, 0, 2, 1), sample2 = c(0.3, 3, 2, 0.4, 2, 3), sample3 = c(0.2, 1, 3, 0.1, 3, 3), sample4 = c(0.4, 2, 4, 0.3, 1, 1), sample5 = c(0.1, 2, 4, 0.2, 5, 3), sample6 = c(0.2,…
Pietro
  • 41
  • 4
1
vote
1 answer

Aggregate a huge data frame: Sum of every five columns

I am working with a huge data frame without column names that I need to aggregate into a specific format. The original data frame has 4915 observations of 1140 variables. I need to aggregate the dataset to a sum of every five variables in the set…
Melike
  • 15
  • 4
1
vote
2 answers

Rowwise sum of subset of columns, column names are stored in another column

Maybe the title doesn't make it too clear, but I didn't know how to formulate it better. I develop the question: I am working in r with a data frame like the following but much larger: df <- data.frame(ref = c("01","02","03","04","05"), …
Rolotoni
  • 43
  • 4
1
vote
4 answers

rowSums() to count number of both non-missing and unique values

Let's say I have this dataframe > df mr_daterd mr_daterd_fu1 mr_daterd_fu2 1 2018-03-05 2018-03-05 2 2019-05-04 2020-03-05 3 2020-01-03 2020-06-06 2021-04-02 Each row represent a patient and the dates…
cmirian
  • 2,572
  • 3
  • 19
  • 59
1
vote
2 answers

Is there a way to use get row sums with specified variables

I would like to get the sum rows but excluding a given row. Listing all the variables is a bit tedious. I would like to highlight by position the variable to exclude in the operation. Any idea? library(tidyverse) df <- tribble( ~"var1", ~"var2",…
Moses
  • 1,391
  • 10
  • 25
1
vote
1 answer

How can I sum votes by category from the randomForest predict function in R?

This example code creates a dataframe with the initial column representing the majority vote from the 10 trees. The next 10 columns contain the categorical vote of each tree in the model. I want to create a chart to show the distribution of votes…
1
vote
2 answers

Is there a reason RowSums(df[grep wouldn't work accurately?

I used df$Total.P.n <- rowSums(df[grep('p.n', names(df), ignore.case = FALSE)]) to sum count values from any column name containing p.n, but the values it produced are way off. The columns are counts of certain combinations of language types in a…
Sarah P.
  • 11
  • 2
1
vote
3 answers

Is there any way to replace values of df in R using sum of rows?

I have an issue which looks like easy to solve, but I'm stuck. I have a dataframe composed of columns (significant pathways retrieved from GSEA) and rows (entrez gene ids). In this data frame there are 1 if a gene is present in a pathway or 0 when…
necrosnake
  • 41
  • 6
1
vote
1 answer

How to find sum of certain rows in R to get a grand total per row?

I have a dataset that has employees' capacity each month, and I want to get a total for each employee across all months: library(dplyr) data <- tibble(employee = c("Justin", "Corey","Sibley", "Justin", "Corey","Sibley"), education =…
J.Sabree
  • 2,280
  • 19
  • 48
1
vote
2 answers

Using mutate, if_else, and rowSums to create a new var based on condition

I have a data frame which is in long format. I have multiple cities. Each of the cities has information for each month and information also for each code (this code goes from 100 up to 1,000). My dataframe looks like…
1
vote
1 answer

Conditionally replace NA in one column by subtracting the sum of other columns from the total

UPDATE: dat1 <- dat %>% mutate(across(starts_with("Number"), ~case_when(is.na(.) ~ Total - sum(.[complete.cases(.)]), TRUE ~ .))) performed the calculation but the result is incorrect. The first row, if calculated correctly, should…
hnguyen
  • 772
  • 6
  • 17
1
vote
1 answer

change row value to 0 based on rowsums in r

here is my data frame: ID A B C D E F 1 0 1 3 5 4 2 2 0 0 0 0 1 0 3 1 2 3 4 4 2 4 0 0 1 1 0 0 I want to get a new data frame based on row sums, if row sums<10, all value in this row changed to 0, which should be like: ID A B C D E F 1 0 1 3…
Feixiang Sun
  • 117
  • 6
1
vote
1 answer

Colsums using loop from indices in list in r

Hello I have a DF with multiple columns all containing numeric values. My df contains over 200 columns but the sample should do. I would like to take the values from the list of indices and using them in a RowSums loop so that the list name is the…
d3hero23
  • 380
  • 1
  • 12
1
vote
2 answers

Aggregate observations across samples by rownames (dplyr) in R

Aloha, I am trying to get the total counts for each row name in my sample matrix. For some reason, I have tried both rowsum and then converting to a data frame and using dplyr::group_by but they are giving errors. Here is a subset of example…
KC Ray
  • 77
  • 9