Search code examples
rdummy-variable

How to view dummy variables


I have a variable (FTA) that has 2 options (yes or no), and I want to create a dummy variable to replace it with yes=1 and no=0. From time period (t) 3 and onwards, it should equal 1, and before that should be 0.

df<-dummy.data.frame(df, names=c("FTA"), sep="_")

After inputting this line of code, I can't see any difference from before when I view the summary of the data (it still counts the number of no's and yes's in the column below the variable name).

I also tried doing:

dummy <- as.numeric(t >= 3)

dummy2 <- as.numeric(t < 3)

As well as:

ifelse(t >=3, 1, 0)

But I still can't observe any changes in the summary. Have I done this correctly, and what can I do to view the dummy variable I created and to replace the old one with it?

Edit: Example of data

My goal is to create a dummy variable that replaces "FTA".


Solution

  • Is this what you want? (Based on the value 4 as the critical watershed in the OP)

    # Data:
    t <- c(1:10)
    FTA <- sample(c("yes", "no"), 10, replace = T)
    df <- data.frame(t, FTA)
    df
        t FTA
    1   1 yes
    2   2 yes
    3   3 yes
    4   4  no
    5   5  no
    6   6  no
    7   7 yes
    8   8  no
    9   9 yes
    10 10 yes
    
    # Change `FTA` based on two conditions:
    df$new <-ifelse(df$t >= 4 &df$FTA=="yes", 1, 
                ifelse(df$t >= 4 &df$FTA=="no", 0, as.character(df$FTA)))
    df
        t FTA new
    1   1 yes yes
    2   2 yes yes
    3   3 yes yes
    4   4  no   0
    5   5  no   0
    6   6  no   0
    7   7 yes   1
    8   8  no   0
    9   9 yes   1
    10 10 yes   1