Reputation: 585
I have following data
Sample_ID<-c("a1","a2","a3","a4","a5","a6")
Score<-c(100, 200, 300, 400, NA, NA)
DF<-data.frame(Sample_ID,Score)
How to create a new variable called Score_status
where all the samples having NA (missing value) will be coded as 0 and those having scores will be coded as 1. I am looking for following output.
Sample_ID Score Score_status
a1 100 1
a2 200 1
a3 400 1
a4 NA 0
a5 NA 0
How to do this in R.
Upvotes: 0
Views: 406
Reputation: 72593
You could use transform
, and convert !is.na
to binary numbers using +
.
DF <- transform(DF, Score_status=+!is.na(Score))
DF
# Sample_ID Score Score_status
# 1 a1 100 1
# 2 a2 200 1
# 3 a3 300 1
# 4 a4 400 1
# 5 a5 NA 0
# 6 a6 NA 0
Upvotes: 2
Reputation: 33498
DF$Score_status <- 1
DF$Score_status[is.na(DF$Score)] <- 0
# Sample_ID Score Score_status
# 1 a1 100 1
# 2 a2 200 1
# 3 a3 300 1
# 4 a4 400 1
# 5 a5 NA 0
# 6 a6 NA 0
Upvotes: 2
Reputation: 39585
You can try:
DF$Score_status <- ifelse(is.na(DF$Score),0,1)
Sample_ID Score Score_status
1 a1 100 1
2 a2 200 1
3 a3 300 1
4 a4 400 1
5 a5 NA 0
6 a6 NA 0
Upvotes: 3