开发者

Change specific column values in R

开发者 https://www.devze.com 2022-12-13 05:20 出处:网络
I have a table where there are \"NA\"s littered all over the place in one column in particular. I want to replace every instance of \"NA\" with something else -- say, the number 1.

I have a table where there are "NA"s littered all over the place in one column in particular. I want to replace every instance of "NA" with something else -- say, the number 1.

How should I do 开发者_如何学Pythonthat?


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

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

For completeness using Deducer's 'Recode Variables' dialog, which can do much more complicated recodings, produces the following code.

> library(Deducer)
> dat[c("a")] <- recode.variables(dat[c("a")] , "NA -> 1;")


x[is.na(x)] <- 1


Update Tablename set column='1' where column='NA'

0

精彩评论

暂无评论...
验证码 换一张
取 消