mercredi 25 janvier 2017

Pulling value that meets multiple conditions from different sized data frames

I have the following dataframes:

df1 <- data.frame(ProjectID=c(10,11,12,13),
              Value1=c(101.25,102.85,102.95,103.15),
              Value2=c(103.58,104.27,104.68,106.01))
df2 <- data.frame(ProjectID=c(10,10,11,11,11,12,13,13),
              Value3=c(98.32,102.58,99.66,103.47,105.63,105.18,102.02,104.98))

I would like to create the following column df1$Value4, which pulls from df2$Value3 if the following conditions are met:

  1. The ProjectIDs must match in df1 & df2
  2. df2$Value3 must be in between df1$Value1 & df1$Value2
  3. If the above 2 conditions are not met, input ""

The output should look like this:

df1 <- data.frame(ProjectID=c(10,11,12,13),
              Value1=c(101.25,102.85,102.95,103.15),
              Value2=c(103.58,104.27,104.68,106.01),
              Value4=c(102.58,103.47,"",104.98))

Aucun commentaire:

Enregistrer un commentaire