Change the column values ​​in R

I have a table in which there is an "NA", littered all over one column, in particular. I want to replace each instance of "NA" with something else - say, number 1.

How can I do it?

+3
source share
3 answers

Jonathan has the correct answer for a vector, which you can apply to column a in the data frame using:

> dat<-data.frame(a=c(11,2,11,NA),b=c(1,1,1,1))
> dat$a[is.na(dat$a)] <- 1

To fully utilize the Deducer 'Recode Variables' dialog, which can perform much more complex re-writes, the following code is issued.

> library(Deducer)
> dat[c("a")] <- recode.variables(dat[c("a")] , "NA -> 1;")
+5
source
x[is.na(x)] <- 1
+4
source

Update column with column name = '1', where column = 'NA'

0
source

Source: https://habr.com/ru/post/1724908/


All Articles