Re: [R] missing and replace

2017-04-27 Thread Fraser D. Neiman
at.columbia.edu/~gelman/arm/missing.pdf best, Fraser From: Val [valkr...@gmail.com] Sent: Wednesday, April 26, 2017 8:45 PM To: r-help@R-project.org (r-help@r-project.org) Subject: [R] missing and replace HI all, I have a data frame with three variabl

Re: [R] missing and replace

2017-04-26 Thread Ng Bo Lin
Apologies, I re-read the question and realised you hope to replace the missing values rounded to the nearest whole number. Here’s the code in full. df1 <- data.frame(x = c(25, 30, 40, 26, 60), y = c(122, 135, NA, 157, 195), z = c(352, 376, 350, NA, 360)) means <- sapply(df1, mean, na.rm = T);

Re: [R] missing and replace

2017-04-26 Thread Ng Bo Lin
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, n

[R] missing and replace

2017-04-26 Thread Val
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