Search code examples
rdataframedplyrsubsetlubridate

Extract values from a DataFrame based on condition on another DataFrame in R


I have following two sample dataframes:

df1 <- data.frame(EVI_GT=c(0.23, 0.54, 0.36, 0.92), EVI_GNT=c(0.33, 0.65, 0.42, 0.73), EVI_GGT=c(0.43, 0.34, 0.22, 0.98))
df2 <- data.frame(T_ET_GT=c(0.56, 0.23, 0.95, 0.82), T_ET_GNT=c(0.10, 0.74, 0.36, 0.35), T_ET_GGT=c(0.52, 0.31, 0.65, 0.58))

I have to extract values from df2 corresponding to min and max of df1 (each row). For example, min (max) value of first row in df1 is 0.23 (0.43) i.e., column 1 (column 3) so the values that should be extracted from df2 will be 0.56 and 0.52 for the first row. Similar for row 2 and so on. Below is my desired output dataframe:

df3 <- data.frame(column1=c(0.56, 0.31, 0.65, 0.35), column2=c(0.52, 0.74, 0.36, 0.58))

How can we get df3 from df2 using conditions on df1?


Solution

  • You can use which.min and which.max to get index of minimum and maximum value respectively. Use apply to perform rowwise operation and subset the data from df2.

    data.frame(column1 = df2[cbind(1:nrow(df1), apply(df1, 1, which.min))],
               column2 = df2[cbind(1:nrow(df1), apply(df1, 1, which.max))])
    
    #  column1 column2
    #1    0.56    0.52
    #2    0.31    0.74
    #3    0.65    0.36
    #4    0.35    0.58