Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R : adding the values in a [row,column] only if value is true in two rows within the same column

I am trying to code the following

I have 240 rows and 11 columns. I want to do the following: If the value in (row 1, column 1) was greater than 0 and if the value in (row 1, column 2) was greater than 0, then count the value in (row 1, column 4).

For an example: (row,column)

If (1,1) = (1) AND if (1,2)=(1) then count the value in (1,4)

If (2,1) = (1) AND if (2,2)=(0) then DO NOT COUNT the value in (2,4)

If (3,1) = (0) AND if (3,2)=(1) then DO NOT COUNT the value in (3,4)

etc...

I have already seen a quite similar post, however there was only 1 condition given instead of 2.

I tried the following solution, which did not work sum(DataFrame_a[DataFrame_a[ ,1] > 0, DataFrame_a[ ,2] > 0, 4])

Thank you in advance for your help.

like image 254
Matthias Herrmann Avatar asked Dec 02 '25 06:12

Matthias Herrmann


1 Answers

your only true condition is:

If (1,1) = (1) AND if (1,2)=(1) then count the value in (1,4)

I don't understand count the value

maybe this solution:

dplyr::mutate(new=ifelse(col1==1&col2==1,col4,0)

gives you a new column Hope that solves your problem

like image 178
Peter Hahn Avatar answered Dec 03 '25 22:12

Peter Hahn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!