Closed chelseachristie closed 3 years ago
I fixed it! I think what happened was that I was mixing up labels and levels. The 'levels' of the variable were 0,1,2,3 and the 'labels' are "15-19", "20-29", "30-39", and "40+". When I used the code below to turn the variable into a factor, it worked.
df$mom_age_cat <- factor(df$mom_age_cat,
levels = c(0,1,2,3),
labels = c("15-19", "20-29", "30-39", "40+"),
ordered = TRUE)
And then I could make a boxplot of birthweight stratified by maternal age category:
ggboxplot(df, x = "mom_age_cat", y = "tgrams")
Great job!!!
Happy to continue to help troubleshooting. Kudos for trying it out on your own dataset!
Andrew
Just revisiting this and I thought of another way to fix. This coding of 0,1,2 etc is very common when importing a file from SPSS. You can "recode" these variables within the column which can simplify your life. If you want an example of this you can upload the file on the Discussion page with Data Wrangling challenges and I'll work on it this afternoon
Andrew
Hi Andrew,
I'm trying out the skills from module 05 with some birth data that I imported from a Stata file. I have a four-level categorical variable for maternal age (age 15-19, 20s, 30s, or age 40+) that was a "numeric" type and I tried to make it a "factor" instead. It looks like it worked, but then it won't let me group infant birth weight (tgrams) by this maternal age variable (mom_age_cat). It looks like all the mom_age_cat values are missing? But the dataset I imported had no missing data on this variable...
Trying to turn it into a factor
ggboxplot(df, x = "mom_age_cat", y = "tgrams")