I am trying to create dummy variables (should be 1/0) using a loop based on the most common variable response. After many searches, I could not find a solution. I extracted the most common answers (lines, for example, the top 5 - "A", "B", ..., "E") using
top5<-names(head(sort(table(data$var1), decreasing = TRUE),5)
I would like the loop to check if another variable ("var2") A is equal, if it is set = 1, OW = 0, then give a summary using aggregate (). In Stata, I can refer to the looped variable i using `i ', but not in R ... Code that doesn't work:
for(i in top5) { data$i.dummy <- ifelse(data$var2=="i",1,0) aggregate(data$i.dummy~data$age+data$year,data,mean) }
Any suggestions?
source share