Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RowSums conditional on value

I'm rather new to r and have a question that seems pretty straight-forward. I want to do rowSums but to only include in the sum values within a specific range (e.g., higher than 0).

e.g. - with the last column being the requested sum

  col1  col2  col3  col4 totyearly
1   -5     3     4    NA      7
2    1     40   -17   -3     41
3   NA     NA    -2   -5      0
4   NA     1      1    1      3  

What I currently have is:

df$totyearly <- rowSums(df[, 1:4], na.rm=TRUE)

How do I add the condition re positive values?

like image 265
user10141392 Avatar asked Aug 16 '26 19:08

user10141392


2 Answers

We can use replace to replace the values less than 0 to 0 and then take rowSums.

df$totyearly <- rowSums(replace(df, df < 0, 0), na.rm = TRUE)
df

#  col1 col2 col3 col4 totyearly
#1   -5    3    4   NA         7
#2    1   40  -17   -3        41
#3   NA   NA   -2   -5         0
#4   NA    1    1    1         3
like image 95
Ronak Shah Avatar answered Aug 18 '26 10:08

Ronak Shah


You could write your own custom sum function and apply it to each row:

df <- read.table(text = "
  col1  col2  col3  col4 totyearly
  1   -5     3     4    NA      7
  2    1     40   -17   -3     41
  3   NA     NA    -2   -5      0
  4   NA     1      1    1      3",
header = TRUE)

#define custom sum function 
sum.pos <- function(x) sum(x[x > 0], na.rm = TRUE)

#apply it to each row
df$totyearly <- apply(df[ , 1:4], 1, sum.pos)

#or equivalently
df$totyearly <- apply(df[ , 1:4], 1, function(x) sum(x[x > 0], na.rm  = TRUE))
like image 35
Chris Holbrook Avatar answered Aug 18 '26 10:08

Chris Holbrook