Perform rowsums using a list of variable and using a condition

Hello,
I am wondering if it is possible to compute a rowsums using a list, and also, filter the sum by a condition from a third variable.
I thought in something as



ghj : data frame
list_g=("va1","va2",var3")

ghj$jkj[ ghj$var4==10 ]=apply(ghj[,list_g],1,rowsums)

The syntax before clearly doesn't work.
As you see, I want to sum var1,var2,var3 only if var4==10.
It seems simple, but I can't figure it out.
Can you guide me?
Thanks for your time and consideration

A good example should include data that the R reader will be able to access. So i chose iris dataset that I expect you to have.

library(tidyverse) 
list_g <- c("Petal.Length","Sepal.Length")
df_to_rowsum <- select(iris,list_g)

rowsum_result <- rowsum(df_to_rowsum,rep(TRUE,nrow(df_to_rowsum)))

# more tidyverse like

rowsum_result2 <- summarise_all(df_to_rowsum,
                                sum)

#> rowsum_result
#    Petal.Length Sepal.Length
#TRUE        563.7        876.5
#> rowsum_result2
#  Petal.Length Sepal.Length
#1        563.7        876.5

Thanks, irgrahamuk
But what I desired was a bit more complex.
Suppose, using iris dataset, that I want the rowsum of Sepal.Length, Sepal.Width, and Petal.Length only if Petal.Width is between 0,8 and 1.2.

I applied the next code

zax=datasets::iris
list_g <- c("Petal.Length","Sepal.Width","Petal.Length")
zax$rowsum_result2=ifelse((zax$Petal.Length >=0.8 & zax$Petal.Length <=1.2), rowSums(zax[,list_g]),NA)
table(zax$rowsum_result2)

It seems to work. However, my doubt now is how can you do the same process using the sapply/apply command and not a conditional. Or, in other word, how to include the exception rule in a sapply/apply order.

Again, thanks for your time and interest.

I just don't think apply/sapply etc are the best tools for such jobs

library(tidyverse)
list_g <-c("Sepal.Length","Sepal.Width","Petal.Length")

my_result <- filter(iris,
                    between(Petal.Width,0.8,1.2)) %>% 
  summarise_at(list_g,sum)

#> my_result
#Sepal.Length Sepal.Width Petal.Length
#          83        37.7         57.3

this book is highly recommended

I tried your code, but It just collapse the sum vertically.
It should produce a vector with many NA, and only in some, where the condition is met, a value.

It was hard to understand what you wanted but I think I understood now

library(tidyverse)
list_g <-c("Sepal.Length","Sepal.Width","Petal.Length")

my_result <- iris %>% 
  mutate(rowsum_of_list_g= ifelse(between(Petal.Width,0.8,1.2),
                                  rowSums(.[list_g]),NA))

(show_result <- arrange(my_result,desc(rowsum_of_list_g)) %>% head(n=20))
Results

# Sepal.Length Sepal.Width Petal.Length Petal.Width    Species rowsum_of_list_g
# 1           6.1         2.8          4.7         1.2 versicolor             13.6
# 2           5.7         3.0          4.2         1.2 versicolor             12.9
# 3           5.8         2.7          4.1         1.0 versicolor             12.6
# 4           5.5         2.6          4.4         1.2 versicolor             12.5
# 5           5.8         2.7          3.9         1.2 versicolor             12.4
# 6           5.8         2.6          4.0         1.2 versicolor             12.4
# 7           6.0         2.2          4.0         1.0 versicolor             12.2
# 8           5.6         2.5          3.9         1.1 versicolor             12.0
# 9           5.7         2.6          3.5         1.0 versicolor             11.8
# 10          5.5         2.4          3.8         1.1 versicolor             11.7
# 11          5.5         2.4          3.7         1.0 versicolor             11.6
# 12          4.9         2.4          3.3         1.0 versicolor             10.6
# 13          5.0         2.3          3.3         1.0 versicolor             10.6
# 14          5.1         2.5          3.0         1.1 versicolor             10.6
# 15          5.0         2.0          3.5         1.0 versicolor             10.5
# 16          5.1         3.5          1.4         0.2     setosa               NA
# 17          4.9         3.0          1.4         0.2     setosa               NA
# 18          4.7         3.2          1.3         0.2     setosa               NA
# 19          4.6         3.1          1.5         0.2     setosa               NA
# 20          5.0         3.6          1.4         0.2     setosa               NA

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.