I'm taking an Advanced Business Analysis class for school and we're learning to program in R Studio.
The professor shared a hint to help us solve a problem, but I'm unable to get it to work.
I'm trying to set the mean height by gender for any height values that contain NA.
Here's what the professor shared as a solution to the problem, but it doesn't work. Nothing gets updated in the data table:
data$height[is.na(data$height) && data$gender == "female"] = data$height[data$gender=="female"]
I tried this:
data$height[is.na(data$height) && data$gender == "female"] = mean(data$height[data$gender=="female"])
and this:
data$height[is.na(data$height) && data$gender == "female"] = mean(data$height[data$gender=="female"], na.rm = TRUE)
But got this error:
In mean.default(data$height[data$gender == "female"]) : argument is not numeric or logical: returning NA
I calculated the mean height of each gender and tried it this way, but that didn’t work either. In all scenarios, the height still displays “NA”.
femaleMeanHeight = mean(data$height[data$gender=="female"], na.rm = TRUE)
data$height[is.na(data$height) && data$gender == "female"] = femaleMeanHeight
I don't know where else to go. Any help is greatly appreciated.