jeudi 25 avril 2019

How Can I merge specific rows from two datasets in R?

I got two different datasets.

The first one(DF1) looks like that: ID schooltype1 date1 date2

the second one(DF2) contains ID schooltype2 date3

Now I want to combine those rows where ID is the same and date3 is in between date1 and date2

I tried to construct a for loop

DF2$school3<-NA
for (i in 1:nrow(DF2)){
  if((DF1$ID==DF2$ID)&(DF2$date3>=DF1$date1)&(DF2$date3<=DF1$date2)){
    DF2$school3<-DF1$schooltype1
  }
}

I need a dataframe that includes ID Schooltype1 School3 date1 date2 date3

Aucun commentaire:

Enregistrer un commentaire