简体   繁体   English

根据来自另一个数据框的日期条件将一个数据框的n个行子集

[英]Subset n number of rows from a dataframe based on date criterias from another dataframe

I have a dataframe (say Summary) in R: 我在R中有一个数据框(例如Summary):

Begin Date | EndDate | Month | Year | Count
2/1/2014 | 1/31/2015 | Jan | 2014 | 10
3/1/2014 | 2/28/2015 | Feb | 2014 | 10
4/1/2014 | 3/31/2015 | Mar | 2014 | 10
5/1/2014 | 4/30/2015 | Apr | 2014 | 10

Another dataframe (say terms) 另一个数据框(例如术语)

Student_Hire_Date | Student_ID
2/1/2014 | 100001
2/2/2014 | 100002
2/3/2014 | 100003
2/4/2014 | 100004
2/5/2014 | 100005

I need an R code that will populate the column "Count", by taking a subset of data from the Terms table where Student_Hire_Date is between 'BeginDate' and 'EndDate' from Summary Table. 我需要一个R代​​码,该代码将通过从“条款”表中获取数据的子集来填充“计数”列,其中Student_Hire_Date在“摘要”表中的“ BeginDate”和“ EndDate”之间。

An R code equivalent to : R代码等效于:

Summary$Count <- "select count(*) from Terms,Summary 
                  where Terms.Student_Hire_Date between
                      Summary.BeginDate and Summary.EndDate"
                  GROUP BY Summary.EndDate

I tried using the following code, but I am getting the same count for every date range. 我尝试使用以下代码,但每个日期范围的计数都相同。

summary$Count <- nrow(subset(Terms, 
                              !is.na(Student_ID)
                               & Student_Hire_Date >= as.Date(Summary$BeginDate)
                                & Student_Hire_Date <= as.Date(Summary$EndDate)

Please help!!!!!! 请帮忙!!!!!!

Consider sapply to loop through each row to compare Summary row value to Terms row value. 考虑通过sapply遍历每一行,以将“ 摘要”行值与“ 条款”行值进行比较。 Right now you are subsetting the same condition for every row of Summary since the BeginDate and EndDate do not match outside row of assigned new column. 现在,您正在为Summary的每一行设置相同的条件,因为BeginDateEndDate与分配的新列的外部行不匹配。

Summary$Count <- sapply(seq_len(nrow(Summary)), function(i) {
       nrow(subset(Terms, !is.na(Student_ID)
                   & Student_Hire_Date >= Summary$BeginDate[[i]]
                   & Student_Hire_Date <= Summary$EndDate[[i]]))
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM