如何使用R提供每周个人资料?

时间:2016-07-16 00:24:50

标签: r for-loop data-analysis bigdata

我得到了这样的旅行交易数据集(约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

1 个答案:

答案 0 :(得分:1)

这可以使用函数xtabs在基础R中完成,但如果我们使用dplyrtidyr包进行此操作可能会更清楚一些。使用此方法,weekday被创建为R因子变量。然后使用dplyr函数mutateDAY转换为因子,将START_TIME转换为整数。接下来,我们使用complete包中的tidyr创建一个新的展开数据框,其中包含IDDAYSTART_TIME每个值的行完整的值范围(例如,每个ID的行,0:23中的每个开始时间以及一周中的每一天。DATEORIGIN和{{的值1}}用于它们存在的位置;否则DESTINATIONDATE, ORIGIN,列的值为DESTINATION。每NAID, 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)