Reputation: 37
I am working with a dataset in R and have some missing values. I am trying to figure out how to create a new variable (z) that follows the following logic "if Y has a missing value then Z's output is 0, if Y does not have a missing value then the output is one. Note, please see example below.
(Original Output
x----y
1 abc
2 svc
3 (blank)
4 ads
5 (blank)
6 adf
(Desired Output)
x----y---------z
1 abc 1
2 svc 1
3 (blank) 0
4 ads 1
5 (blank) 0
6 adf 1
Upvotes: 0
Views: 1060
Reputation: 26
Depending on whether the blank values are NA
or string , you need to use
df$z <- ifelse(is.na(df$y), 0, 1)
Say if string contains the word 'blank'
df$z <- ifelse(df$y=='blank', 0, 1)
Upvotes: 0
Reputation: 703
Make sure your missing values in y
are coded as NA
in your data frame and then you can assign a new column z
to your data frame df
using the ifelse
function.
df$z <- ifelse(is.na(df$y), 0, 1)
Upvotes: 1