How to compare two columns in R data frame and return 0 or 1 in the third column based on the comparison?

后端 未结 2 1040
梦谈多话
梦谈多话 2021-01-29 14:49

I have a dataframe with two columns(both are dates) and a million rows. I have to compare both the dates and return value in the third column. i.e if date in column A is greater

相关标签:
2条回答
  • 2021-01-29 14:58

    In base:

    DF$C <- as.numeric(DF$A > DF$B)
    

    In dplyr:

    DF %>% 
      mutate(C = as.numeric(A > B))
    
    0 讨论(0)
  • 2021-01-29 15:16
    library(data.table)
    dt <- as.data.table(dt)
    dt$A <- as.Date(dt$A)
    dt$B <- as.Date(dt$B)
    

    Here are two ways you can try:

    dt[, C := ifelse(A > B, 1, 0)]
    

    or

    dt[, C := 0][A > B, C := 1]
    

    In second way, you can change to dt[, C := 1][A <= B, C := 0] by checking which has less obs.

    Maybe you need to provide a little reproducible example.

    0 讨论(0)
提交回复
热议问题