本文介绍了R使用data.table来剪切包含2个或更多变量的固定时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资料框架

df <- data.frame(time = c("2015-09-07 00:32:19", "2015-09-07 01:02:30", "2015-09-07 01:31:36", "2015-09-07 01:47:45",
"2015-09-07 02:00:17", "2015-09-07 02:07:30", "2015-09-07 03:39:41", "2015-09-07 04:04:21", "2015-09-07 04:04:21", "2015-09-07 04:04:22"), 
inOut = c("IN", "OUT", "IN", "IN", "IN", "IN", "IN", "OUT", "IN", "OUT")) 

> df
                  time inOut
1  2015-09-07 00:32:19    IN
2  2015-09-07 01:02:30   OUT
3  2015-09-07 01:31:36    IN
4  2015-09-07 01:47:45    IN
5  2015-09-07 02:00:17    IN
6  2015-09-07 02:07:30    IN
7  2015-09-07 03:39:41    IN
8  2015-09-07 04:04:21   OUT
9  2015-09-07 04:04:21    IN
10 2015-09-07 04:04:22   OUT
> 

我想计算每15分钟IN / OUT的计数,
可以通过创建另一个in_df,out_df,每15分钟剪切这些数据帧,然后将这些合并在一起,以获得我的结果。 outdf是我的预期结果。

I want to calculate the number of counts for IN/OUT per 15 mins,I can do this by creating another in_df, out_df, cut these dataframe per 15 mins, and then merge this together to obtain my result. The outdf is my expected result.

in_df <- df[which(df$inOut== "IN"),]
out_df <- df[which(df$inOut== "OUT"),]

a <- data.frame(table(cut(as.POSIXct(in_df$time), breaks="15 mins")))
b <- data.frame(table(cut(as.POSIXct(out_df$time), breaks="15 mins")))
colnames(b) <- c("Time", "Out")
colnames(a) <- c("Time", "In")

outdf <- merge(a,b, all=TRUE)
outdf[is.na(outdf)] <- 0

> outdf
                  Time In Out
1  2015-09-07 00:32:00  1   0
2  2015-09-07 00:47:00  0   0
3  2015-09-07 01:02:00  0   1
4  2015-09-07 01:17:00  1   0
5  2015-09-07 01:32:00  0   0
6  2015-09-07 01:47:00  2   0
7  2015-09-07 02:02:00  1   0
8  2015-09-07 02:17:00  0   0
9  2015-09-07 02:32:00  0   0
10 2015-09-07 02:47:00  0   0
11 2015-09-07 03:02:00  0   0
12 2015-09-07 03:17:00  0   0
13 2015-09-07 03:32:00  1   0
14 2015-09-07 03:47:00  0   0
15 2015-09-07 04:02:00  1   2

我的问题是如何做data.table,以获得相同的结果?

My questions is how to do this with data.table to obtain the same result?

推荐答案

在data.table中,我会做

In data.table, I would do

library(data.table)
setDT(df)

df[, timeCut := cut(as.POSIXct(time), breaks="15 mins")]

df[J(timeCut = levels(timeCut)), 
   as.list(table(inOut)), 
   on = "timeCut", 
   by = .EACHI]



which gives:

                timeCut IN OUT
 1: 2015-09-07 00:32:00  1   0
 2: 2015-09-07 00:47:00  0   0
 3: 2015-09-07 01:02:00  0   1
 4: 2015-09-07 01:17:00  1   0
 5: 2015-09-07 01:32:00  0   0
 6: 2015-09-07 01:47:00  2   0
 7: 2015-09-07 02:02:00  1   0
 8: 2015-09-07 02:17:00  0   0
 9: 2015-09-07 02:32:00  0   0
10: 2015-09-07 02:47:00  0   0
11: 2015-09-07 03:02:00  0   0
12: 2015-09-07 03:17:00  0   0
13: 2015-09-07 03:32:00  1   0
14: 2015-09-07 03:47:00  0   0
15: 2015-09-07 04:02:00  1   2

说明最后一部分是 DT [i = J(x = my_x ),j,on =x,by = .EACHI] ,可以读作:

Explanation The last part is like DT[i=J(x=my_x), j, on="x", by=.EACHI] and can be read as:


  1. my_x 上加入 DT x

  2. 然后对 my_x 确定的每个子集执行 j

  1. Join DT column x on my_x.
  2. Then do j on each subset determined by my_x.

在这种情况下, j = as.list(table(inOut))。该表必须被强制到列表以创建多个列(对于 inOut 中的每个级别一个列)。

In this case, j=as.list(table(inOut)). The table has to be coerced to a list to create multiple columns (one for each level of inOut).

这篇关于R使用data.table来剪切包含2个或更多变量的固定时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:13