Search code examples
rrecode

High and low objects using R's car library


I'm trying to add a column which reads my dataframe's column and outputs a 1 if the element is bigger than a certain number (and a zero if the condition isn't met). However, this code doesn't seem to work: df is an existing dataframe.

df2 <- data.frame(df2, C=Recode(df$numbers, "hi:200=1; else=0")) ##C = numbers > 200 = 1

I'm using R's car library.


Solution

  • Does this achieve what you need?

    df2 <- tibble(numbers = c(1, 200, 201))
    
    df2$recoded <- ifelse(df2$numbers > 200, 1, 0)
    
    df2
    
    # # A tibble: 3 x 2
    # numbers recoded
    # <dbl>   <dbl>
    #   1       1       0
    #   2     200       0
    #   3     201       1