How to compare two columns in R data frame and ret

2020-05-10 07:00发布

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 than date in column B, return 1 in column C.

Thanks in advance :)

标签: r
2条回答
成全新的幸福
2楼-- · 2020-05-10 07:33

In base:

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

In dplyr:

DF %>% 
  mutate(C = as.numeric(A > B))
查看更多
趁早两清
3楼-- · 2020-05-10 07:36
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.

查看更多
登录 后发表回答