vendredi 25 juin 2021

Nested for-loop wiht if statement in R

Hi this is the code that I am trying to run and it's taking awhile. districts is a data frame of 39299 rows and 16 columns and lm_data is a data frame of 59804 rows and 16 variables. I want to set up a new variable in lm_data called tentativeStartDate which takes on the value of districts$firstDay[j] if a couple of conditions are meant. Is there a more efficient way to do this? Thanks in advance!

''' for (i in 1: nrow(lm_data)){ for (j in 1: nrow(districts)){ if (lm_data$DISTORGID[i] == districts$DISTORGID[j] & lm_data$gradeCode[i] == districts$gradeCode[j]){ lm_data$tentativeStartDate[i] = districts$firstDay[j] } } } '''

Aucun commentaire:

Enregistrer un commentaire