I am trying to create a new column (variable) according to the values that appear in an existing column such that if there is an NA in the existing column then the corresponding value in the new column should be 0 (zero), if not NA then it should be 1 (one). An example data is given below:
aid=c(1,2,3,4,5,6,7,8,9,10)
age=c(2,14,NA,0,NA,1,6,9,NA,15)
data=data.frame(aid,age)
My new data frame should look like this:
aid=c(1,2,3,4,5,6,7,8,9,10)
age=c(2,14,NA,0,NA,1,6,9,NA,15)
surv=c(1,1,0,1,0,1,1,1,0,1)
data<-data.frame(aid,age,surv)
data
I hope that my question is clear enough.
The R community's help is highly appreciated!
Baz
surv = 1 - is.na(age)
> data
aid age surv
1 1 2 1
2 2 14 1
3 3 NA 0
4 4 0 1
5 5 NA 0
6 6 1 1
7 7 6 1
8 8 9 1
9 9 NA 0
10 10 15 1
>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With