문제
StudentNum=1:10
English=c(70,NA,80,85,90,90,60,55,NA,90)
Math=c(80,73,84,90,95,85,60,50,NA,100)
data=data.frame(StudentNum,English,Math)
data
## StudentNum English Math
## 1 1 70 80
## 2 2 NA 73
## 3 3 80 84
## 4 4 85 90
## 5 5 90 95
## 6 6 90 85
## 7 7 60 60
## 8 8 55 50
## 9 9 NA NA
## 10 10 90 100
위의 성적 data를 사용하여 젠제누락된 값을 각 과목의 평균치로 채워 넣으세요.
apply(data,2,function(x){ifelse(is.na(x),mean(x,na.rm=TRUE),x)})
## StudentNum English Math
## [1,] 1 70.0 80.00000
## [2,] 2 77.5 73.00000
## [3,] 3 80.0 84.00000
## [4,] 4 85.0 90.00000
## [5,] 5 90.0 95.00000
## [6,] 6 90.0 85.00000
## [7,] 7 60.0 60.00000
## [8,] 8 55.0 50.00000
## [9,] 9 77.5 79.66667
## [10,] 10 90.0 100.00000