【问题标题】:Count event occurrence and assign it to individuals according to date and place of interview根据采访的日期和地点统计事件发生并分配给个人
【发布时间】:2017-11-04 20:45:06
【问题描述】:

我在 R 中有两个数据框:A.dfB.df。第一个包含 N 行,每行是在特定日期和地点发生的事件。

第二个是在特定日期和地点接受采访的个人列表。

对于每个人,我想统计在面试日期之前的某个时间范围内,在个人面试地点的同一地点发生的事件的数量。

假设时间范围是面试日期前 x 天,我已经计算出该日期并存储在变量 xdaysbefore 中。

下面是数据框的样子

A.df

#Event     Date     Place
   1   2015-05-01     1    
   2   2015-03-11     1
   3   2015-07-04     2
   4   2015-05-10     3

B.df

#Individual  Date of Interview   Place   xdaysbefore
       1         2016-07-11        1       2014-09-11  
       2         2016-05-07        3       2014-07-04
       3         2016-08-09        2       2014-03-22
       4         2016-01-10        3       2014-09-17

注意DateDate of Interviewxdaysbefore都在Date R class

我如何根据事件发生的地点和采访的个人地点为B.df 中的每个人计算在Date of Interview - xdaysbefore 时间范围内发生的事件。

我对@9​​87654335@ 的期望如下所示:

B.df

#Individual  Date of Interview   Place   xdaysbefore      CountedEvents
       1         2016-07-11        1       2014-09-11         2
       2         2016-05-07        3       2014-07-04         1
       3         2016-08-09        2       2014-03-22         1
       4         2016-01-10        3       2014-09-17         1

其中CountedEvents 是在Date of Interview - xdaysbefore 时间范围内以及在我接受采访的同一地点发生的事件数。

【问题讨论】:

  • 你能提供一个reproducible example(我们可以复制/粘贴到R中的东西)吗?
  • 所以基本上,您想在B.df 中查找一个位置,在同一位置xdaysbefore to Date Of interview 范围内A.df 中发生的事件数。

标签: r date dataframe count


【解决方案1】:

您可以在B.df 的每一行上使用apply

A.df 的一个子集,其中位置相等。检查A.df中的Date是否在Date_of_Interviewxdaysbefore的范围内

B.df$CountedEvents <- apply(B.df, 1, function(x) {
    temp = A.df[A.df$Place %in% x[3],]
    length(temp$Date < as.Date(x[2]) & temp$Date > as.Date(x[4]))
 })

B.df
#     Individual Date_of_Interview Place xdaysbefore CountedEvents
#1          1        2016-07-11     1      2014-09-11       2
#2          2        2016-05-07     3      2014-07-04       1
#3          3        2016-08-09     2      2014-03-22       1
#4          4        2016-01-10     3      2014-09-17       1

编辑

如果你想使用名称而不是索引来访问列,你可以使用

apply(B.df, 1, function(x) {
        temp = A.df[A.df$Place %in% x["Place"],]
        length(temp$Date < as.Date(x["Date_of_Interview"]) & 
               temp$Date > as.Date(x["xdaysbefore"]))
})

【讨论】:

  • 谢谢,它有效。如果我想改进它以便只计算某种类型的事件怎么办?假设在 A.df 中有一个附加列“EventType”,它显示值 1、2、3、4。我只想计算类型 1 的事件。
  • temp = A.df[A.df$Place %in% x[3],]改成temp = A.df[A.df$Place %in% x[3] &amp; A.df$EventType ==1,]
  • 由于我的数据集中有很多变量,我想按名称调用它们。我认为下面的代码有问题。它生成变量 CountedEvents,没有变化,所有行只有一个值,该值对应于 B.df 中的观察数。代码:B.df$CountedEvents &lt;- apply(B.df, 1, function(x) { temp = A.df[A.df$Place %in% B.df$Place,] length(temp$Date &lt; as.Date(B.df$date) &amp; temp$Date &gt; as.Date(B.df$xdaysbefore)) })
  • @HamidOskorouchi 好的..我已经更新了答案。请检查这是否是您想要的。
【解决方案2】:

您可以通过结合使用mergeaggregate 来实现:

# merge into a new dataset
AB <- merge(A, B, by = 'Place', all = TRUE)

# create a logical variable which indicates whether 'Date' falls within the range
AB$count <- AB$xdaysbefore < AB$Date & AB$Date_of_Interview > AB$Date

# aggregate into a count varaible
aggregate(count ~ Individual + Date_of_Interview + xdaysbefore, AB, sum)

给出:

  Individual Date_of_Interview xdaysbefore count
1          3        2016-08-09  2014-03-22     1
2          2        2016-05-07  2014-07-04     1
3          1        2016-07-11  2014-09-11     2
4          4        2016-01-10  2014-09-17     1

或者,您可以使用data.table 包的development version 中的新非等连接可能性:

library(data.table)

# convert the dataframes to data.table's (which are enhanced dataframes)
setDT(A)
setDT(B)

# join and count
A[B, on = .(Place, Date < Date_of_Interview, Date > xdaysbefore)
  ][, .(count = .N), .(Individual, Place, Date_of_Interview = Date, xdaysbefore = Date.1)]

给出:

   Individual Place Date_of_Interview xdaysbefore count
1:          1     1        2016-07-11  2014-09-11     2
2:          2     3        2016-05-07  2014-07-04     1
3:          3     2        2016-08-09  2014-03-22     1
4:          4     3        2016-01-10  2014-09-17     1

【讨论】:

    猜你喜欢
    • 2021-03-22
    • 2019-08-12
    • 2016-10-07
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多