计算R中多个变量的实例

时间:2015-04-03 23:14:26

标签: r dplyr

我有一个大型数据表Divvy(超过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将其连接回初始表,使其成为如下所示的内容,然后将其限制为不同于from_station_id / to_combos,我将用它来映射路径(我的每个站点都有lat / long):

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来获取其中一些内容,例如:

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

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

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]&amp; n [1]等...

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

我是R的新手,我已经四处寻找/认为我已经接近了,但我无法将最后一点加入Divvy。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

由于您说'#34;将其限制为与from_station_id / to_combos&#34;不同,以下代码似乎提供了您所追求的内容。您的数据称为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

答案 2 :(得分: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