vendredi 23 février 2018

multiple ifelse with and and or conditions in R

I have a dataset and I want to create a multiple ifelse statement. I want to find out if this person/id lived in the big city (being london or paris) in the years 2011 and 2012. Any suggestions how I can get this with multiple ifelse?

 id<-  c(1,1,2,2,3)
 location <- c('london', 'paris', 'london', 'kathmanadu','newyork')
 year<- c(2011, 2010, 2012, 2011, 2010)
 df<- data.frame(location, year)
 df$bigcity<- ifelse(df$location=='london'| df$location=='paris' & 
 df$year==2011| df$year==2012, 1, 0)

Aucun commentaire:

Enregistrer un commentaire