R 使用sqldf包创建时间间隔
这就是我的数据框的外观 我想创建15分钟或30分钟的时间间隔,并为该时间间隔内的所有时间戳计算R 使用sqldf包创建时间间隔,r,time,timestamp,dataframe,sqldf,R,Time,Timestamp,Dataframe,Sqldf,这就是我的数据框的外观 我想创建15分钟或30分钟的时间间隔,并为该时间间隔内的所有时间戳计算No_Words的总和。我需要这个来绘制每个时间间隔的平均字数 我该怎么做呢 另外,我很想知道使用sqldfpackage是否可以找到解决方案 Time No_Words 1 2013-11-17 13:37:00 6 2 2013-11-17 13:37:00
No_Words
的总和。我需要这个来绘制每个时间间隔的平均字数
我该怎么做呢
另外,我很想知道使用sqldf
package是否可以找到解决方案
Time No_Words
1 2013-11-17 13:37:00 6
2 2013-11-17 13:37:00 16
3 2013-11-17 13:37:00 18
4 2013-11-17 13:37:00 12
5 2013-11-17 14:03:00 5
6 2013-11-17 14:03:00 20
7 2013-11-17 14:04:00 4
8 2013-11-17 17:21:00 39
9 2013-11-17 22:48:00 19
10 2013-11-17 22:48:00 12
这个答案不是使用sqldf,而是使用基本R函数
aggregate
和cut
:
## If your "Time" column is not an actual time object,
## convert it to one before proceeding.
mydf$Time <- as.POSIXct(mydf$Time)
这个答案不是使用sqldf,而是使用基本R函数
aggregate
和cut
:
## If your "Time" column is not an actual time object,
## convert it to one before proceeding.
mydf$Time <- as.POSIXct(mydf$Time)
使用的简单方法,例如tapply
:
agg <- tapply(df$No_Words, list(groups), mean)
par(mar=c(2,10,0,0)); barplot(agg, horiz=TRUE, las=1)
agg
使用的简单方法,例如tapply
:
agg <- tapply(df$No_Words, list(groups), mean)
par(mar=c(2,10,0,0)); barplot(agg, horiz=TRUE, las=1)
aggsqldf这里是一个sqldf解决方案,其中输入数据帧是DF
:
library(sqldf)
min15 <- 15 * 60 # in seconds
ans <- fn$sqldf("select
t.Time - t.Time % $min15 as Time,
sum(t.No_Words) as No_Words
from DF t
group by Time")
plot(No_Words ~ Time, ans, type = "o")
使用稠密网格如果需要稠密网格,那么我们需要一个网格数据框,G
,它与先前的ans
(请注意,sqldf拉入了chron包,因此我们使用了它的trunc
功能):
zoo我们还展示了一个zoo解决方案:
library(zoo)
library(chron)
FUN <- function(x) as.POSIXct(trunc(as.chron(x), 15 / (24 * 60)))
z <- read.zoo(DF, FUN = FUN, aggregate = sum)
plot(z)
注意:我们使用了这些数据,尤其是时间属于“POSIXct”
:
linesqldf这里是一个sqldf解决方案,其中输入数据帧是DF
:
library(sqldf)
min15 <- 15 * 60 # in seconds
ans <- fn$sqldf("select
t.Time - t.Time % $min15 as Time,
sum(t.No_Words) as No_Words
from DF t
group by Time")
plot(No_Words ~ Time, ans, type = "o")
使用稠密网格如果需要稠密网格,那么我们需要一个网格数据框,G
,它与先前的ans
(请注意,sqldf拉入了chron包,因此我们使用了它的trunc
功能):
zoo我们还展示了一个zoo解决方案:
library(zoo)
library(chron)
FUN <- function(x) as.POSIXct(trunc(as.chron(x), 15 / (24 * 60)))
z <- read.zoo(DF, FUN = FUN, aggregate = sum)
plot(z)
注意:我们使用了这些数据,尤其是时间属于“POSIXct”
:
行
> head(ans2)
Time No_Words
1 2013-11-17 13:30:00 52
2 2013-11-17 13:45:00 0
3 2013-11-17 14:00:00 29
4 2013-11-17 14:15:00 0
5 2013-11-17 14:30:00 0
6 2013-11-17 14:45:00 0
library(zoo)
library(chron)
FUN <- function(x) as.POSIXct(trunc(as.chron(x), 15 / (24 * 60)))
z <- read.zoo(DF, FUN = FUN, aggregate = sum)
plot(z)
> z
2013-11-17 13:30:00 2013-11-17 14:00:00 2013-11-17 17:15:00 2013-11-17 22:45:00
52 29 39 31
Lines<- " Time No_Words
1 2013-11-17 13:37:00 6
2 2013-11-17 13:37:00 16
3 2013-11-17 13:37:00 18
4 2013-11-17 13:37:00 12
5 2013-11-17 14:03:00 5
6 2013-11-17 14:03:00 20
7 2013-11-17 14:04:00 4
8 2013-11-17 17:21:00 39
9 2013-11-17 22:48:00 19
10 2013-11-17 22:48:00 12
"
raw <- read.table(text = Lines, skip = 1)
DF <- data.frame(Time = as.POSIXct(paste(raw$V2, raw$V3)), No_Words = raw$V4)