0

I have variable a in a dataset b

NA
NA
NA
53
53
NA

I want the output as

1
1
1 
53
53
1

I tried

b$a1[i1] <- ifelse(b$a[i1]=="NA", 1,b$a[i1]) 

But it is not getting replace. Format of a is list

suresh
  • 33
  • 5

2 Answers2

3

The answer you are looking for is

b$a[is.na(b$a)] <- 1
ArunK
  • 1,731
  • 16
  • 35
0

We can use replace

 with(b, replace(a, is.na(a), 1))
akrun
  • 874,273
  • 37
  • 540
  • 662