计算 R 中多个变量的实例

Counting Instances of Multiple Variables in R

我有一个大数据 table 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 将其加入初始的 table 以制作类似下面的内容,然后将其限制为不同的 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(Divvy$from_station_id.x==94 & Divvy$to_station_id.x == 69)
  x    freq
1 FALSE 2454553
2  TRUE      81

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

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]))

应该可以,但想到它仍然只有 return 300 行,而不是 44k,所以它必须然后循环并执行 n[2] & n[1] 等。 .

我觉得可能还有一个更快的 dplyr 解决方案可以让我 return 计算每个组合并直接附加它而无需额外的 steps/table 创建,但我还没有找到它。

我是 R 的新手,我搜索过 around/think 我很接近,但我不能完全将连接该结果的最后一个点与 Divvy 联系起来。任何帮助表示赞赏。

我不完全确定这就是您要查找的结果,但这会计算出具有相同起点和目的地的行程数。如果这不是您期望的最终结果,请随时发表评论并告诉我。

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
#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

既然你说了"limit it to distinct 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