【发布时间】:2025-12-13 09:20:05
【问题描述】:
我有两个数据框,一个具有唯一 ID,另一个具有多个 ID。如果在第一个数据帧日期或之前的第二个数据帧中的值的值为 1,并且如果第二个数据帧中缺少 id 我想分配 NA,我想在第一个数据帧中创建另一列。在 R 中最有效的方法是什么?
# first dataframe with each unique id
set.seed(123)
df <- data.frame(id = c(1:7),
date = seq(as.Date("2020/12/26"),
as.Date("2021/1/1"), by = "day"))
#s second dataframe with repeated id
df1 <- data.frame(id = rep(1:5, each = 5),
date = sample(seq(as.Date('2020/12/20'),
as.Date('2021/1/15'), by="day"), 25),
assign = sample(c(0,1), replace=TRUE, size=25))
df1 <- arrange(df1,id, date)
# the output that I want
df$response <- c(1,0,0,1,0,NA,NA)
【问题讨论】:
-
是的,我更新了回复
标签: r dplyr data.table