I need help. I have a dataframe in R with some columns NA (I know it's uninformated column) and would like to add information in that position, but I have no idea how to do this. Need help. Thank you !!
I need help. I have a dataframe in R with some columns NA (I know it's uninformated column) and would like to add information in that position, but I have no idea how to do this. Need help. Thank you !!
The best way to give an example of data, in this case a data.frame
is to use the command dput
:
dput(dat) # postar o output disto
Let's first produce a% example.
set.seed(2174)
dat <- data.frame(X <- rnorm(10),
A = sample(letters[1:4], 10, TRUE),
stringsAsFactors = FALSE)
dat$X[c(2,4,7)] <- NA
Now we have everything we need. Suppose we have data.frame
for valor
.
inx <- is.na(dat$X)
dat$X[inx] <- valor # 'valor' deve ser numérico
That's all. Simple, is not it?
Hello @Bianca, try the coalesce function of the dplyr package. It replaces all NA's with the indicated value, in the example below it is zero:
x <- sample(c(1:5, NA, NA, NA))
dplyr::coalesce(x, 0L)