my variable"age" looks like this, which is an age group.
but when I am trying to create age's dummy variable with the code
age1=ifelse(sr$age=="75+",1,0)
sr=data.frame(sr,age1)
and trying to add it to the dataset, the new variable "age1" doesn't show me "1" when age is "75+"
I think there may be some question in recognizing "age" as a categorical variable.
How should I deal with it?
Could you please turn this into a self-contained REPRoducible EXample (reprex)? Because I can't reproduce your issue, your code works as expected for me.
sr <- data.frame(stringsAsFactors = FALSE,
age = as.factor(c("75+", "55-74", "35-54", "25-34", "15-24", "5-14")),
value = 1:6)
age1 <- ifelse(sr$age=="75+",1,0)
sr <- data.frame(sr,age1)
sr
#> age value age1
#> 1 75+ 1 1
#> 2 55-74 2 0
#> 3 35-54 3 0
#> 4 25-34 4 0
#> 5 15-24 5 0
#> 6 5-14 6 0