nattys
nattys

Reputation: 235

Sum values from rows with conditions in R

I need to create a new column with the sum values in several other columns, but with conditions.

My data is

ID <- c(A,B,C,D,E,F)
Q1 <- c(0,1,7,9,na,3) 
Q2 <- c(0,3,2,2,na,3) 
Q3 <- c(0,0,7,9,na,3) 

dta <- as.data.frame (ID,Q1,Q2,Q3) 

I need to sum values from the columns only if the values are < 4. If there is any value in any column that is > 4, the result should be dismissed. And I need to preserve the rows with only "na".

The result should look like

Result 
0
4
na
na
na
9

I have tried :

library(dplyr)
dta %>% filter(Q1 < 4) %>% mutate(Result = rowSums(.[2:4]))

but then, all the rows with values > 4 disappear, and I was only able filter one row at a time. I have also tried:

dta$Result <- ifelse(c("Q1", "Q2", "Q3") < 4, rowSums(.[2:4]), NA)

but then all my results are "na"

Upvotes: 0

Views: 1235

Answers (2)

Anoushiravan R
Anoushiravan R

Reputation: 21908

You can use the following solution:

library(dplyr)

dta %>%
  rowwise() %>%
  mutate(Result = ifelse(any(c_across(Q1:Q3) > 4), NA, Reduce(`+`, c_across(Q1:Q3))))

# A tibble: 6 x 5
# Rowwise: 
  ID       Q1    Q2    Q3 Result
  <chr> <dbl> <dbl> <dbl>  <dbl>
1 A         0     0     0      0
2 B         1     3     0      4
3 C         7     2     7     NA
4 D         9     2     9     NA
5 E        NA    NA    NA     NA
6 F         3     3     3      9

Upvotes: 0

c0bra
c0bra

Reputation: 1080

ID <- c("A","B","C","D","E","F")
Q1 <- c(0,1,7,9,NA,3) 
Q2 <- c(0,3,2,2,NA,3) 
Q3 <- c(0,0,7,9,NA,3) 

dta <- data.frame(ID,Q1,Q2,Q3)

You have to switch the sum and ifelse statement.

dta %>% 
   rowwise() %>%
   mutate(result = sum(ifelse(c(Q1, Q2, Q3)<4, c(Q1, Q2, Q3), NA)))

Upvotes: 1

Related Questions