gpt4 book ai didi

r - 选择数据 - 第一个条目 + 设置时间段(1 年)R

转载 作者:行者123 更新时间:2023-12-02 18:04:06 33 4
gpt4 key购买 nike

我有一个关于一组个人的数据集,该数据集是在每个人的不同时间开始收集的。

我需要对自第一次输入以来一年内的数据进行子集化,如下所示:myData[myDate >= "first entry" & myDate += "1 year"]

示例数据:

df_date <- data.frame( Name = c("Jim","Jim","Jim","Jim","Jim","Jim","Jim","Jim","Jim","Jim","Jim","Jim","Jim","Jim",
"Sue","Sue","Sue","Sue","Sue","Sue","Sue","Sue","Sue","Sue","Sue","Sue","Sue","Sue"),
Dates = c("2010-1-1", "2010-2-2", "2010-3-5","2010-4-17","2010-5-20",
"2010-6-29","2010-7-6","2010-8-9","2010-9-16","2010-10-28","2010-11-16","2010-12-28","2011-1-16","2011-2-28",
"2010-4-1", "2010-5-2", "2010-6-5","2010-7-17","2010-8-20",
"2010-9-29","2010-10-6","2010-11-9","2012-12-16","2011-1-28","2011-2-28","2011-3-28","2011-2-28","2011-3-28"),
Event = c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1) )

所需的输出是 Jim 将从 1/1/2010 - 12/28/2010 获取数据和苏来自 4/4/2010 - 3/28/2011等等。实际数据集有超过 20 个样本,全部在不同时间开始。

最佳答案

结合使用 tidyverselubridate 函数:

library(tidyverse)
library(lubridate)

df_date %>%
mutate(Dates = as_datetime(Dates)) %>%
group_by(Name) %>%
arrange(Dates, .by_group = T) %>%
filter(Dates <= first(Dates) + duration(1, units = "year"))

关于r - 选择数据 - 第一个条目 + 设置时间段(1 年)R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73704912/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com