Tyler Witt
Tyler Witt

Reputation: 37

Using If then logic in R to create a new variable

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

Answers (3)

Hemant Gupta
Hemant Gupta

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

ThomasIsCoding
ThomasIsCoding

Reputation: 101247

Try the code below

df$z <- +nzchar(df$y)

Upvotes: 0

Dave Ross
Dave Ross

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

Related Questions