使用 Countifs & Lookups 的等价物在 R 中汇总数据
Using equivalents of Countifs & Lookups to summarise data in R
我有一个交易数据文件(位置之间的行程),我希望使用 R 对其进行总结,我对它比较陌生。
示例数据
Start.Date Start.Area End.Date End.Area
2007-07-12 14:00 New Street 2007-07-12 15:46 Windy Lane
2007-07-12 15:10 High Street 2007-07-12 18:08 New Street
2007-07-12 16:42 Kings Street 2007-07-12 17:47 Windy Lane
我的目标是 return 一个地区每天(可能是一小时)的出现次数。
示例 Return,在新的 data.frame 中,将是
Date Area Start.Occurances End.Occurances
2007-07-12 New Street 1 1
2007-07-12 High Street 1 0
2007-07-12 Kings Street 1 0
2007-07-12 Windy Lane 0 2
理想情况下,我会在 Excel 中进行分析,但它无法处理我的数据规模。在电子表格中,我会使用 countif 函数来查看该区域在给定 date/time 中出现了多少次。
如果可能,我也希望合并 Start.Occurances 和 End.Occurances 均为零的天数。
我已经看到的关于 Countif 函数或 Match/Index 组合的问题没有解决我的问题,所以我希望有人能帮助我!
这个可以先整形再总结。这是一个使用 reshape2
和 dplyr
的示例(数据为 dat
)。
## First reshape the data
library(reshape2)
m <- melt(dat, id.vars=c("Start.Date", "End.Date"),
value.name = "Area", variable.name="Area.Pos")
## Summarise, grouping by Area
library(dplyr)
m %>% group_by(Area) %>%
summarise(Start.Occurences = sum(Area.Pos == "Start.Area"),
End.Occurences = sum(Area.Pos == "End.Area"))
# Area Start.Occurences End.Occurences
# 1 High Street 1 0
# 2 Kings Street 1 0
# 3 New Street 1 1
# 4 Windy Lane 0 2
另一种方法:将 'Start.Date' 和 'Start.Area' 列堆叠在相应的 'End' 列之上,将列重命名为 'Date' 和 'Area'新列 'Pos',指定它是 'Start' 还是 'End'。然后很容易通过分组区域、日期或两者进行汇总。
m <- rbind(`names<-`(dat[,grep("Start", names(dat))], c("Date", "Area")),
`names<-`(dat[,grep("End", names(dat))], c("Date", "Area")))
m$Pos <- rep(c("Start", "End"), each=nrow(dat))
m %>% group_by(as.Date(Date), Area) %>%
summarise(Start.Occurences = sum(Pos == "Start"),
End.Occurences = sum(Pos == "End"))
as.Date(Date) Area Start.Occurences End.Occurences
# 1 2007-07-12 High Street 1 0
# 2 2007-07-12 Kings Street 1 0
# 3 2007-07-12 New Street 1 1
# 4 2007-07-12 Windy Lane 0 2
我有一个交易数据文件(位置之间的行程),我希望使用 R 对其进行总结,我对它比较陌生。 示例数据
Start.Date Start.Area End.Date End.Area
2007-07-12 14:00 New Street 2007-07-12 15:46 Windy Lane
2007-07-12 15:10 High Street 2007-07-12 18:08 New Street
2007-07-12 16:42 Kings Street 2007-07-12 17:47 Windy Lane
我的目标是 return 一个地区每天(可能是一小时)的出现次数。
示例 Return,在新的 data.frame 中,将是
Date Area Start.Occurances End.Occurances
2007-07-12 New Street 1 1
2007-07-12 High Street 1 0
2007-07-12 Kings Street 1 0
2007-07-12 Windy Lane 0 2
理想情况下,我会在 Excel 中进行分析,但它无法处理我的数据规模。在电子表格中,我会使用 countif 函数来查看该区域在给定 date/time 中出现了多少次。
如果可能,我也希望合并 Start.Occurances 和 End.Occurances 均为零的天数。
我已经看到的关于 Countif 函数或 Match/Index 组合的问题没有解决我的问题,所以我希望有人能帮助我!
这个可以先整形再总结。这是一个使用 reshape2
和 dplyr
的示例(数据为 dat
)。
## First reshape the data
library(reshape2)
m <- melt(dat, id.vars=c("Start.Date", "End.Date"),
value.name = "Area", variable.name="Area.Pos")
## Summarise, grouping by Area
library(dplyr)
m %>% group_by(Area) %>%
summarise(Start.Occurences = sum(Area.Pos == "Start.Area"),
End.Occurences = sum(Area.Pos == "End.Area"))
# Area Start.Occurences End.Occurences
# 1 High Street 1 0
# 2 Kings Street 1 0
# 3 New Street 1 1
# 4 Windy Lane 0 2
另一种方法:将 'Start.Date' 和 'Start.Area' 列堆叠在相应的 'End' 列之上,将列重命名为 'Date' 和 'Area'新列 'Pos',指定它是 'Start' 还是 'End'。然后很容易通过分组区域、日期或两者进行汇总。
m <- rbind(`names<-`(dat[,grep("Start", names(dat))], c("Date", "Area")),
`names<-`(dat[,grep("End", names(dat))], c("Date", "Area")))
m$Pos <- rep(c("Start", "End"), each=nrow(dat))
m %>% group_by(as.Date(Date), Area) %>%
summarise(Start.Occurences = sum(Pos == "Start"),
End.Occurences = sum(Pos == "End"))
as.Date(Date) Area Start.Occurences End.Occurences
# 1 2007-07-12 High Street 1 0
# 2 2007-07-12 Kings Street 1 0
# 3 2007-07-12 New Street 1 1
# 4 2007-07-12 Windy Lane 0 2