Not Available (how to put information in this position)

Asked

Viewed 63 times

0

I need help. I have a dataframe in R with some columns IN (I know it’s a column without information) and would like to add information in this position, but I have no idea how to do it. I need help. Thank you!!

  • 1

    Bianca, the question is very generic. What kind of information should replace the NA? Is it something you’ve already defined? Or you don’t know what information should be added, wishing to perform an imputation of data through some mathematical criterion?

2 answers

1

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 data.frame 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 a valor for the NA.

inx <- is.na(dat$X)
dat$X[inx] <- valor  # 'valor' deve ser numérico

That’s it. Simple, no?

1

Hello @Bianca, try the coalesce function of the dplyr package. It replaces all Nas with the given value, in the example below is zero:

x <- sample(c(1:5, NA, NA, NA))
dplyr::coalesce(x, 0L)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.