dimanche 14 février 2021

r - alternative to IF statement to apply dummy variables with time series data

I am currently undertaking a project where I have to analyse government speech data (long story). What I am looking to do is create a dummy variable if a party is in government - which I have been able to do in a time period where only one party has been in government using this:

parl_data$government <-ifelse(parl_data$party=="PartyName",1,0)

However, within the full dataframe there are changes of the party in government. My data includes 'speech IDs' and by using the dates alongside, I can see where there were changes in government, and such what I would like to do is do the following:

IF (speech_ID <= x) {<br>
parl_data$government <-ifelse(parl_data$party=="PartyName",1,0)<br>
} else {<br>
parl_data$government <-ifelse(parl_data$party=="DifferentPartyName",1,0)<br>
}

As Im sure you know using IF returns the following error

"the condition has length > 1 and only the first element will be used"

how do I complete the above using other means?

Thanks in Advance

Aucun commentaire:

Enregistrer un commentaire