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
0
votes
2 answers

Adding new column to dataframe (R) with a rowsum

Edit: The lecturer suggested using Stack Overflow to seek out community supports and leverage the community when we move into data analytics. I'm a mature student and this is a career pivot attempt. My goal is to learn and folks are great - thank…
iKcon
  • 71
  • 7
0
votes
2 answers

Define thresholds based on different categories with RowSum

df <- data.frame(PatientID = c("0002" ,"0002", "0005", "0005" ,"0009" ,"0009" ,"0018", "0018" ,"0039" ,"0039" , "0043" ,"0043", "0046", "0046" ,"0048" ,"0048"), Timepoint= c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A",…
Lili
  • 547
  • 6
  • 19
0
votes
1 answer

Calculating net value of a row with condition on the column

My dataframe has 2695 observations of 195 variables, and its first 100x10 quadrant looks like this: structure(list(name = c("Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan",…
nflore
  • 196
  • 1
  • 10
0
votes
1 answer

R for loop wise : Rowwise sum on conditions : Performance issue

I have a database, where I am running code to change value of a cell-based on the sum of previous cells and the sum of succeeding cells in the same row. for (i in 1:row1) { for(j in 3:col-1) { # for-loop over columns …
Sandy
  • 3
  • 3
0
votes
2 answers

R: find number of columns > 0 per row for a group of column names with a partial string match

I have a dataframe that resembles the following: ID X Y A_1_l A_2_m B_1_n B_2_l C_1_m C_2_n C_3_l w X Y 0 0 0 0 0 0 0 x X Y 0 0 3 0 0 0 0 y X Y 0 1 0 4 0 1 0 z X Y 3 4 5 6 2 1 5 The first letter denotes a sample, the number a…
keenan
  • 462
  • 3
  • 12
0
votes
1 answer

How do I calculate sum of rows (apply on every subject) and divided by a number in R?

I'm trying to calculate sum or a row in R divided by group number and apply that on every subject (n = 102). For example, in the excel sheet, I'm trying to calculate the inclination of choosing organic food in the fruits category (organic = 1,…
0
votes
2 answers

In R. conditionally adding values where one of the variables has to be positive (using rowsums)

I have used the following code previously to add values of a row: subset$EBIT <- rowSums(subset[c("rorresul", "resand", "rteinknc", "rteinext", "rteinov")], na.rm = TRUE) However, I would actually need…
0
votes
0 answers

Sum columns based specific row and sum rows based on specific column as new variable

I want to construct a 2x2 contingency table to compute the odds ratio. However, I fail to sum up the rows and columns as I wanted them to be. This is my contingency table for now. I want to total up Down Steady and Up into a new variable named…
0
votes
1 answer

How can I sum multiple columns by rows in a dataframe?

How can I sum the columns (C1.dn + C2.dn + V1.dn + V2.dn) by each row and display it as a new column in the same dataframe? For example, sum of row 1 (9.659531 + 5.159067 + 9.220461 + 13.48245) should be displayed as a new column (37.52). A snippet…
Pranav_b
  • 19
  • 8
0
votes
1 answer

R Get all subsets where rowsum fulfills condition

I have a data.table in R for example: x <- data.table( id = c(1:10), count=c(10,110,20,30,5,40,50,15,20,70)) I want to select all subsets respectively combinations of id where the rowsum of count is between 90 and 110. One combination would be…
Judy
  • 35
  • 5
0
votes
0 answers

Counting string matches according to a target vector across rows in a data frame

I have a large dataset (~520,000 rows and 1,000 columns). A subset of those columns are ICD codes. I would like to perform a sum for each row (among the subset of columns that contain ICD codes) that counts all of the columns for which the ICD code…
Austin
  • 11
  • 4
0
votes
1 answer

R conditional rowSums to replace with sums based on percentage

I'm looking to conditionally rowSums if those rows represent <1% of the data - and then replace the original values with the rowSums. *Bonus if the table could include the number of rows that were summed into the name column (e.g., "Other(n=2)").…
0
votes
0 answers

large rowSums() results in Inf ? large number problem in R

I have a great data.matrix and I want to calculate the sum of the rows. Using rowSums function results in Inf values for sum because (presumably) the numbers are too large. So I tried using Brobdingnagian numbers (from Brobdingnagian package,…
JORIS
  • 59
  • 7
0
votes
1 answer

use select in rowsums

dataframe %>% select(thumb_p1_frac_1, thumb_p1_frac_2, thumb_p1_frac_3, thumb_p1_frac_4, thumb_p1_frac_5) %>% mutate(P1 = rowSums(., na.rm = TRUE)) above works, but I want to remove the pipe of select and implement select select in mutate,…
0
votes
0 answers

HTML table transfer rows and get sum of row values

I am trying to transfer selected rows from one table to another and than get last column values sum. I use footerCallback to show sum: $(document).ready(function() { $('#table2').DataTable( { "footerCallback": function ( row, data,…