2015-04-03 90 views
4

我有一个大的数据表瓜分(超过240万条记录)(某些列删除):计数实例

X trip_id  from_station_id.x to_station_id.x 
1 1109420  94     69 
2 1109421  69     216 
3 1109427  240     245 
4 1109431  113     94 
5 1109433  127     332 
3 1109429  240     245 

我想找到旅行的人数从每个站到每个相对的站。因此,例如,

From X  To Y  Sum 
94   69  1 
240  245  2 

等,然后使用dplyr做出类似下面,然后加入它回到inital表将其限制为不同from_station_id/to_combos,我将用它来绘制路线(我有经/纬每个站):

X trip_id  from_station_id.x to_station_id.x Sum 
1 1109420  94     69    1 
2 1109421  69     216    1 
3 1109427  240     245    2 
4 1109431  113     94    1 
5 1109433  127     332    1 
3 1109429  240     245    1 

我成功地用于计数,以获得一些这方面,如:

count(Divvy$from_station_id.x==94 & Divvy$to_station_id.x == 69) 
    x freq 
1 FALSE 2454553 
2 TRUE  81 

但是,这显然是劳动密集型的,因为有300个独特的站,超过44k的pos组合。我创建了一个帮助表,以便我可以循环它。

n <- select(Divvy, from_station_id.y) 

    from_station_id.x 
1    94     
2    69     
3    240    
4    113    
5    113    
6    127    

    count(Divvy$from_station_id.x==n[1,1] & Divvy$to_station_id.x == n[2,1]) 

     x freq 
1 FALSE 2454553 
2 TRUE  81 

我觉得自己像一个循环,如

output <- matrix(ncol=variables, nrow=iterations) 


output <- matrix() 
for(i in 1:n)(output[i, count(Divvy$from_station_id.x==n[1,1] & Divvy$to_station_id.x == n[2,1])) 

应该工作,但想起来它仍然只会返回300行,而不是44K,所以它必须接着返回,并做n [2] & n [1] etc ...

我觉得可能还有一个更快的dplyr解决方案,可以让我返回每个组合的数量并直接追加它,而无需额外的步骤/表创建,但我还没有找到它。

我对R更新,我搜索了周围/认为我很近,但我无法连接最后一个加入Divvy的结果点。任何帮助赞赏。

+0

我尝试了所有这三种解决方案,并且我不得不说他们都正确地获得了总和,并以奇妙的方式工作。我使用dplyr选项作为“最佳”选项,因为它能够为我提供我想要的有限数量的行,但我认为data.table选项可能是最优雅的。 – ike 2015-04-10 00:33:45

+0

另外:如果其他人希望看到/使用原始数据集,请访问:http://www.divvybikes.com/data – ike 2015-04-10 00:41:31

回答

4

既然你说 “它限制在不同的from_station_id/to_combos”,下面的代码似乎提供你所追求的。您的数据被称为mydf

library(dplyr) 
group_by(mydf, from_station_id.x, to_station_id.x) %>% 
count(from_station_id.x, to_station_id.x) 

# from_station_id.x to_station_id.x n 
#1    69    216 1 
#2    94    69 1 
#3    113    94 1 
#4    127    332 1 
#5    240    245 2 
+0

我最终使用此为: counts4 < - GROUP_BY(divvydata,trip_id,from_station_id.x,to_station_id.x)%>% 计数(from_station_id.x,to_station_id.x,From_Station_Lat,From_Station_Long,End_Station_Lat,End_Station_Long) – ike 2015-04-10 00:34:24

+1

@ike我很高兴你根据这个建议找到了你自己的解决方案。 :) – jazzurro 2015-04-10 14:04:57

3

我不完全确定这就是你要找的结果,但是这会计算具有相同原点和目的地的旅程的数量。随意评论,让我知道如果这不是你期望的最终结果。

dat <- read.table(text="X trip_id  from_station_id.x to_station_id.x 
1 1109420  94     69 
2 1109421  69     216 
3 1109427  240     245 
4 1109431  113     94 
5 1109433  127     332 
3 1109429  240     245", header=TRUE) 

dat$from.to <- paste(dat$from_station_id.x, dat$to_station_id.x, sep="-") 
freqs <- as.data.frame(table(dat$from.to)) 
names(freqs) <- c("from.to", "sum") 
dat2 <- merge(dat, freqs, by="from.to") 
dat2 <- dat2[order(dat2$trip_id),-1] 

结果

dat2 

# X trip_id from_station_id.x to_station_id.x sum 
# 6 1 1109420    94    69 1 
# 5 2 1109421    69    216 1 
# 3 3 1109427    240    245 2 
# 4 3 1109429    240    245 2 
# 1 4 1109431    113    94 1 
# 2 5 1109433    127    332 1 
+0

这确实很好,谢谢。虽然我做了dat作为read.csv,所以我可以直接导入文件并跳过其他一些步骤。谢谢。 – ike 2015-04-10 00:41:02

5
#Here is the data.table solution, which is useful if you are working with large data: 
library(data.table) 
setDT(DF)[,sum:=.N,by=.(from_station_id.x,to_station_id.x)][] #DF is your dataframe 

    X trip_id from_station_id.x to_station_id.x sum 
1: 1 1109420    94    69 1 
2: 2 1109421    69    216 1 
3: 3 1109427    240    245 2 
4: 4 1109431    113    94 1 
5: 5 1109433    127    332 1 
6: 3 1109429    240    245 2 
+2

这是很好的解决方案。 – 2015-04-04 07:15:35

+0

这很美,谢谢。 – ike 2015-04-10 00:33:58