[R] Subscripting problem with is.na()

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Thu Jun 23 15:57:59 CEST 2016


Hi All,

I would like to recode my NAs to 0. Using a single vector everything is 
fine.

But if I use a data.frame things go wrong:

-- cut --

var1 <- c(1:3, NA, 5:7, NA, 9:10)
var2 <- c(1:3, NA, 5:7, NA, 9:10)
ds_test <-
  data.frame(var1, var2)

test <- var1
test[is.na(test)] <- 0
test  # NA recoded OK

# First try
ds_test[is.na(ds_test$var1)] <- 0  # duplicate subscripts WRONG

# Second try
ds_test[is.na("var1")] <- 0 
ds_test$var1  # not recoded WRONG

# Third try: to me the most intuitive approach
is.na(ds_test["var1"]) <- 0  # attempt to select less than one element in 
integerOneIndex WRONG

# Fourth try
ds_test[is.na(var1)] <- 0  # duplicate subscripts for columns WRONG

-- cut --
 
How can I do it correctly?

Where could I have found something about it?

Kind regards

Georg



More information about the R-help mailing list