Reputation: 99
I have a data set where data are missing. Here is a sample of what my data looks like:
df<-read.csv(id, test1, test2, test3
1, 9, 1, 3
2, 8, 2, NA
3, NA, 3, NA
4, 1, 3, 4
5, 2, 44, NA
6, 4, 4, 1
7, NA, NA, NA)
How would I input the respective mean of each test into the corresponding column for each NA?
Output should look like
id test1 test2 test3
1, 9, 1, 3
2, 8, 2, 2.66
3, 4.8, 3, 2.66
4, 1, 3, 4
5, 2, 44, 2.66
6, 4, 4, 1
7, 4.8, 9.5, 2.66
Upvotes: 0
Views: 23
Reputation: 887118
An option would be na.aggregate
library(zoo)
df[-1] <- na.aggregate(df[-1])
Upvotes: 2