Search code examples
rdataframeparallel.foreachmultiple-conditions

Referencing different data frames(same size) for conditional change of resulting data frame


I want to include values stored in different data frames(same size) into conditions to generate value for the resulting data frame. My data: int contains the intensity of different features (m2, m4, m1e3) measured for a number of people (in rows).

int<-data.frame(int_m2=c(33,32,35) ,int_m4=c(111,113,118), int_m1e3=c(104,99,110))
View(int)

3 different quality evaluation (s_, p_, i_) for each feature (m2, m4, m1e3).

s_<-data.frame(s_m2=rep(8,3) ,s_m4=rep(100,3), s_m1e3=c("NA", 100, 100 ))    
p_<-data.frame(p_m2=rep(10,3), p_m4=rep(10,3), p_m1e3=c("NA", 10, 10 ))
i_<-data.frame(i_m2=rep(0.1,3), i_m4=rep(0.5,3), i_m1e3=c("NA", 0.1, 0.5 ))

I need results in which intensity from int would be pasted if conditions are TRUE for quality evaluation. In the case of "NA" or FALSE value should be 0.

Quality evaluation conditions: if conditions(s_[i,j] >9 & p_[i,j] <20 & i_[i,j]> 0.2

Expected results:

res<-data.frame(m2=c(0,0,0),m4=c(111,113,118), m1e3=c(0, 0, 110 ))

Elaborate ranting: My goal was to apply this on a big data set and to use foreach and dopar to make it faster. But, after trying combinations of mutate, ifelse, sappy, or old school if/else loop I always get stuck on how to make inputs for conditions change if applied on the data frame. All that I found during extensive research are constants as conditions applied over data frames. So each try I had was half-finished code and thus not very useful share. Any recommendations for further reading on the subject are more than welcome.


Solution

  • Instead of looping over i, j, create the multiple logical expressions on the whole dataset, and multiply with int. Those FALSE (-> 0) multiplied returns 0 for corresponding 'int' while the TRUE (-> 1) will return the same value from 'int'

    (s_ > 9 & p_ < 20 & i_ > 0.2) * int
    

    -output

     int_m2 int_m4 int_m1e3
    1      0    111        0
    2      0    113        0
    3      0    118        0