T-T - 1 year ago 87
R Question

# Repeating calculation based on conditions

What I am trying to do is pretty simple. However, I am new to R and have not learned much about loops and functions and am not sure what is the most efficient way to get the results. Basically, I want to count the number of rows that meet my conditions and do a division. Here is an example:

``````df1 <- data.frame(
Main = c(0.0089, -0.050667, -0.030379, 0.066484, 0.006439, -0.026076),
B = c(NA, 0.0345, -0.0683, -0.052774, 0.014661, -0.040537),
C = c(0.0181, 0, -0.056197, 0.040794, 0.03516, -0.022662),
D = c(-0.0127, -0.025995, -0.04293, 0.057816, 0.033458, -0.058382)
)
df1
#    Main       B          C          D
# 1  0.008900   NA         0.018100  -0.012700
# 2 -0.050667   0.034500   0.000000  -0.025995
# 3 -0.030379  -0.068300  -0.056197  -0.042930
# 4  0.066484  -0.052774   0.040794   0.057816
# 5  0.006439   0.014661   0.035160   0.033458
# 6 -0.026076  -0.040537  -0.022662  -0.058382
``````

My criteria for the numerator is to count the number of
`B/C/D`
that is
`>0`
when
`Main`
is
`>0`
; For denominator, count the number of
`B/C/D`
that is
`!= 0`
when
`Main`
is
`!= 0`
. I can use
`length(which(df1\$Main >0 & df1\$B>0)) / length(which(df1\$Main !=0 & df1\$B !=0))`
to get the ratios for each of the column individually. But my data set has many more columns, and I am wondering if there is a way to get those ratio all at once so that my result will be like:

``````#    B          C          D
# 1  0.2        0.6        0.3
``````

`````` apply(df1[,-1], 2, function(x) length(which(df1\$Main >0 & x>0)) / length(which(df1\$Main !=0 & x !=0)))