Aryh
Aryh

Reputation: 585

How to create new column in R based on missing values

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

Answers (3)

jay.sf
jay.sf

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

s_baldur
s_baldur

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

Duck
Duck

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

Related Questions