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

3
  • Use is.na() function for checking whether the value is NA or not Commented May 12, 2016 at 9:02
  • I tried the above is.na(). It showing false for values but not showing true for NA Commented May 12, 2016 at 9:05
  • Can you please include the output of str(b)? Commented May 12, 2016 at 9:10

2 Answers 2

3

The answer you are looking for is

b$a[is.na(b$a)] <- 1
Sign up to request clarification or add additional context in comments.

Comments

0

We can use replace

 with(b, replace(a, is.na(a), 1))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.