我得到了这样的旅行交易数据集(约560000次旅行):dataframe 1
ID START TIME DATE ORIGIN DESTINATION DAY
1005 9.10 2012-01-02 A B Monday
1005 18.15 2012-01-02 B A Monday
1005 9.05 2012-01-08 A B Sunday
1005 17.05 2012-01-08 B A Sunday
1010 8.00 2012-01-09 A C Monday
1010 12.00 2012-01-09 C A Monday
1013 13.15 2012-01-10 D E Tuesday
1013 15.30 2012-01-10 E G Tuesday
1013 9.06 2012-01-12 D E Thursday
... ... 2012-..-.. . . ...
像这样的和ID索引(约1986年ID):Dataframe 2
ID
1005
1010
1013
1015
1030
1034
1036
1031
1040
...
我想根据这两个数据框创建每周旅行资料。我不确定我是否正确,但我尝试了这些代码:
weekday = c("Sunday", "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
br = seq(0,23,by=1)
ranges = paste(head(br,-1), br[-1], sep="_")
for (i in dataframe2$ID) {
for (n in weekday){
x= filter(dataframe1,dataframe1$ID %in% i & dataframe1$DAY %in% n)
freq = hist(as.numeric(x), br, include.lowest=TRUE, plot=FALSE)
df = as.data.frame(t(data.frame(frequency = freq$counts)))
df$i = i
df$n = n
colnames(df) = c(as.character(ranges),"ID","Day")
write.table(head(df),file="testdata1.csv", append=TRUE,sep=",",col.names=FALSE,row.names=FALSE)
}
}
我想最终得到一张包含每周行程频率的csv表。我还想问一下,是否有一种简单的方法来简化这项任务。
ID 0_1 1_2 2_3 3_4 4_5 5_6 6_7 7_8 8_9 9_10 10_11 11_12 12_13 13_14 14_15 15_16 16_17 17_18 18_19 19_20 20_21 21_22 22_23 Day
1005 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 Sunday
1005 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 Monday
1005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Tuesday
1005 Wednesday
1005 Thursday
1005 Friday
1005 Saturday
1010 Sunday
1010
1010
1010
1010
1010
1010
最后我想制作一个这样的图: enter image description here
答案 0 :(得分:1)
这可以使用函数xtabs
在基础R中完成,但如果我们使用dplyr
和tidyr
包进行此操作可能会更清楚一些。使用此方法,weekday
被创建为R因子变量。然后使用dplyr
函数mutate
将DAY
转换为因子,将START_TIME
转换为整数。接下来,我们使用complete
包中的tidyr
创建一个新的展开数据框,其中包含ID
,DAY
和START_TIME
每个值的行完整的值范围(例如,每个ID
的行,0:23中的每个开始时间以及一周中的每一天。DATE
,ORIGIN
和{{的值1}}用于它们存在的位置;否则DESTINATION
和DATE, ORIGIN,
列的值为DESTINATION
。每NA
和ID, DAY,
的行程数计算为START_TIME,
的{{1}}值NA
并且存储在DATE
中的行总和。使用Freq
中的spread
函数将tidyr
的每个不同值转换为单独的列。最后分配正确的列名,将列排列为请求的顺序,将数据框作为csv写入文件。
Freq
您可以使用
进一步总结您的情节数据 library(dplyr)
library(tidyr)
#
# input data is in df
# convert colunm name START TIME to syntactically correct version START_TIME
#
colnames(df)[2] <- "START_TIME"
#
# define weekday as a factor with the days of week
#
weekday <- c("Sunday", "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
weekday <- factor(weekday, levels=weekday)
#
# sum number for trips by ID, DAY, and START_TIME
#
trip_freq <- df %>% mutate(DAY = factor(DAY, levels=levels(weekday)),
START_TIME=floor(START_TIME)) %>%
complete(ID, DAY=weekday, START_TIME=0:23) %>%
group_by(ID, DAY, START_TIME) %>%
summarise(Freq = sum(!is.na(DATE)))
trip_freq_tbl <- trip_freq %>% spread(key = START_TIME, value=Freq)
#
# name and re-arrange columns
#
colnames(trip_freq_tbl) <- c("ID", "Day", paste(0:23,1:24,sep="_"))
trip_freq_tbl <- cbind(trip_freq_tbl[,-2], Day=trip_freq_tbl[,"Day"])
#
# write trip_freq as csv fle
#
write.table(trip_freq_tbl, file="testdata1.csv", sep=",", row.names=FALSE)