I have a dataset containing several variables, two of which are age and wage. They are both numeric variables. I want to create a function in R that will group the variable age as follows:
- less or equal to 35 :“young”
- over 35 and less or equal to 55 : “adult”
- over 55 : “old”
I want to then use the tapply
function to compute the average wage for these 3 groups
This is the code I have:
age<-our_data$age
wage<-our_data$wage
my_function<-function(age){
for(i in 1:length(age)){
if (i <= 35){
i="young"
}
else if(i>35 & i<=55){
i="adult"
}else if(i>55){
i="old"
}
}
}
tapply(wage, my_function(age), mean)
However it is not running. It says arguments must have same length even tho both wage and age have length 534.