. Suppose you have the following data set with missing values: 1 2 3 4 5 NA 7 6 NA 5 4 3 NA 2 6 10 14 NA 4 4 4 NA 10 13 16 19 NA
(a) Give R expressions that return a vector of the data set without missing values
(b) Give R expressions that return a vector of the data set after replacing missing values by the last non-missing values. For example, the last non-missing value of the 1st missing is “5” so replace 1st missing by “5”
> x <-
c(1,2,3,4,5,NA,7,6,NA,5,4,3,NA,2,6,10,14,NA,4,4,4,NA,10,13,16,19,NA);x
[1] 1 2 3 4 5 NA 7 6 NA 5 4 3 NA 2 6 10 14
[18] NA 4 4 4 NA 10 13 16 19 NA
a)
> x1 <- na.omit(x) ; x1
[1] 1 2 3 4 5 7 6 5 4 3 2 6 10 14 4 4 4
[18] 10 13 16 19
attr(,"na.action")
[1] 6 9 13 18 22 27
attr(,"class")
[1] "omit"
> x[6]
[1] NA
b)
> x[c(6,9,13,18,22,27)] <- x[c(5,8,12,17,21,26)]
> x
[1] 1 2 3 4 5 5 7 6 6 5 4 3 3 2 6 10 14
[18] 14 4 4 4 4 10 13 16 19 19
Get Answers For Free
Most questions answered within 1 hours.