Question: Question: I want to replace the missing value for male in class 2 with the median age 27.5, the missing value for female in class
Question: I want to replace the missing value for male in class 2 with the median age 27.5, the missing value for female in class 2 with 40, and the missing value for male in class 3 with 26. How can I do that in R Studio?
median age for male in class 2 is 27.5
median age for female in class 2 is 40
median age for male in class 3 is 26
age <- c(34, 26, 21, NA, NA, 40, 22, 33, NA, 45) gender <- c(1, 2, 1, 1, 1, 2, 2, 1, 2, 1) # 1=male, 2=female class <- c(2, 3, 2, 2, 3, 2, 3, 1, 2, 1) df <- data.frame(age, gender, class)
|
| age | gender | class | |
|---|---|---|---|---|
| 1 | 34 | 1 | 2 | |
| 2 | 26 | 1 | 3 | |
| 3 | 21 | 1 | 2 | |
| 4 | NA | 1 | 2 | |
| 5 | NA | 1 | 3 | |
| 6 | 40 | 2 | 2 | |
| 7 | 22 | 2 | 3 | |
| 8 | 33 | 1 | 1 | |
| 9 | NA | 2 | 2 | |
| 10 | 45 | 1 | 1 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
