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
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.
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.
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.