[R] missing and replace
Ng Bo Lin
ngbolin91 at gmail.com
Thu Apr 27 03:19:26 CEST 2017
Hi Val,
You could do this by nesting 2 for loops, and defining a function such that it returns the mean of the column when the value is ‘NA’.
df1 <- data.frame(x = c(25, 30, 40, 26, 60), y = c(122, 135, NA, 157, 195), z = c(352, 376, 350, NA, 360)); df2 <- df1[0, ]
means <- sapply(df1, mean, na.rm = T); return_mean_if_NA <- function(x, y) { if (is.na(x)){ x <- y } else { return(x) } }
for (i in 1:ncol(df1)){
for (j in 1:nrow(df1)){
df2[j, i] <- return_mean_if_NA(df1[j, i], means[i])
}
}
Hope this helps!
Regards,
Bo Lin
> On 27 Apr 2017, at 8:45 AM, Val <valkremk at gmail.com> wrote:
>
> HI all,
>
> I have a data frame with three variables. Some of the variables do
> have missing values and I want to replace those missing values
> (1represented by NA) with the mean value of that variable. In this
> sample data, variable z and y do have missing values. The mean value
> of y and z are152. 25 and 359.5, respectively . I want replace those
> missing values by the respective mean value ( rounded to the nearest
> whole number).
>
> DF1 <- read.table(header=TRUE, text='ID1 x y z
> 1 25 122 352
> 2 30 135 376
> 3 40 NA 350
> 4 26 157 NA
> 5 60 195 360')
> mean x= 36.2
> mean y=152.25
> mean z= 359.5
>
> output
> ID1 x y z
> 1 25 122 352
> 2 30 135 376
> 3 40 152 350
> 4 26 157 360
> 5 60 195 360
>
>
> Thank you in advance
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list