Bin数据由(x,y)组成并总结

时间:2013-02-27 20:24:29

标签: r dataframe plyr binning summarization

这些是我拥有的大型文件的前10行:(请注意,这10行中只有一个用户,但我有成千上万的用户)

dput(testd)
structure(list(user = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), otime = structure(c(10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L
), .Label = c("2010-10-12T19:56:49Z", "2010-10-13T03:57:23Z", 
"2010-10-13T16:41:35Z", "2010-10-13T20:05:43Z", "2010-10-13T23:31:51Z", 
"2010-10-14T00:21:47Z", "2010-10-14T18:25:51Z", "2010-10-16T03:48:54Z", 
"2010-10-16T06:02:04Z", "2010-10-17T01:48:53Z"), class = "factor"), 
    lat = c(39.747652, 39.891383, 39.891077, 39.750469, 39.752713, 
      39.752508, 39.7513, 39.758974, 39.827022, 39.749934),
    long = c(-104.99251, -105.070814, -105.068532, -104.999073, 
      -104.996337, -104.996637, -105.000121, -105.010853,
      -105.143191, -105.000017),
    locid = structure(c(5L, 4L, 9L, 6L, 1L, 2L, 8L, 3L, 10L, 7L),
      .Label = c("2ef143e12038c870038df53e0478cefc", 
      "424eb3dd143292f9e013efa00486c907", "6f5b96170b7744af3c7577fa35ed0b8f", 
      "7a0f88982aa015062b95e3b4843f9ca2", "88c46bf20db295831bd2d1718ad7e6f5", 
      "9848afcc62e500a01cf6fbf24b797732f8963683", "b3d356765cc8a4aa7ac5cd18caafd393", 
      "d268093afe06bd7d37d91c4d436e0c40d217b20a", "dd7cd3d264c2d063832db506fba8bf79", 
      "f6f52a75fd80e27e3770cd3a87054f27"), class = "factor"),
    dnt = structure(c(10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L),
      .Label = c("2010-10-12 19:56:49", 
      "2010-10-13 03:57:23", "2010-10-13 16:41:35", "2010-10-13 20:05:43", 
      "2010-10-13 23:31:51", "2010-10-14 00:21:47", "2010-10-14 18:25:51", 
      "2010-10-16 03:48:54", "2010-10-16 06:02:04", "2010-10-17 01:48:53"
    ), class = "factor"),
    x = c(-11674.6344476781, -11683.3414552141, 
      -11683.0877083915, -11675.3642199817, -11675.0599906624, 
      -11675.0933491404, -11675.4807522648, -11676.6740962175, 
      -11691.3894104198, -11675.4691879924),
    y = c(4419.73724843345, 4435.719406435, 4435.68538078744,
      4420.05048454181, 4420.3000059572, 4420.27721099723,
      4420.14288752585, 4420.99619739292, 4428.56278976123, 
      4419.99099525605),
    cellx = structure(c(1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L),
      .Label = c("[-11682,-11672)", "[-11692,-11682)"
    ), class = "factor"),
    celly = structure(c(1L, 2L, 2L, 1L, 
      1L, 1L, 1L, 1L, 1L, 1L), .Label = c("[4419,4429)", "[4429,4439)"
    ), class = "factor"),
    cellxy = structure(c(1L, 3L, 3L, 1L, 
      1L, 1L, 1L, 1L, 2L, 1L), .Label = c("[-11682,-11672)[4419,4429)", 
      "[-11692,-11682)[4419,4429)", "[-11692,-11682)[4429,4439)"
    ), class = "factor")), .Names = c("user", "otime", "lat", 
"long", "locid", "dnt", "x", "y", "cellx", "celly", "cellxy"), class = "data.frame", row.names = c(NA, 
-10L))

关于数据是什么以简化理解的一些解释。 x和y是纬度和长坐标的变换。我已使用cut将x,y位置分离为区间。我希望每个用户获得访问量最大的bin,因此我使用ddply。如下:

cells = ddply(testd, .(user, cellxy), summarise, length(cellxy))

获得:

dput(cells)
structure(list(user = c(0, 0, 0), cellxy = structure(1:3, .Label = c("[-11682,-11672)[4419,4429)", 
"[-11692,-11682)[4419,4429)", "[-11692,-11682)[4429,4439)"), class = "factor"), 
    count = c(7L, 1L, 2L)), .Names = c("user", "cellxy", "count"
), row.names = c(NA, -3L), class = "data.frame")

现在我想要做的是计算每个用户访问最多的bin的第一个数据集的平均x,y,这是从前面的计算中获得的。我不知道如何有效地做到这一点,并且鉴于我的数据集非常大,我会很感激一些指导。谢谢!

2 个答案:

答案 0 :(得分:4)

这是两个阶段的方法。首先,修改cells的原始代码 - 对于cellxyuser的每个组合,计算平均xy值。

 cells = ddply(testd, .(user, cellxy), summarise,
       cellcount=length(cellxy),meanx=mean(x),meany=mean(y))
 cells
  user                     cellxy cellcount     meanx    meany
1    0 [-11682,-11672)[4419,4429)         7 -11675.40 4420.214
2    0 [-11692,-11682)[4419,4429)         1 -11691.39 4428.563
3    0 [-11692,-11682)[4429,4439)         2 -11683.21 4435.702

然后使用对ddply()的其他来电为最高cellxy的每个用户cellcount进行子集。

cells2 = ddply(cells,.(user),subset,cellcount==max(cellcount))
cells2
  user                     cellxy cellcount    meanx    meany
1    0 [-11682,-11672)[4419,4429)         7 -11675.4 4420.214

答案 1 :(得分:3)

由于您的数据集很大,您可能需要考虑data.table,它不仅速度快,而且还会使数据更轻松。

转换为数据表是直截了当的:

    library (data.table)
    DT <- data.table(testd, by="user")

然后按用户确定访问次数最多只有一行

    # Determining which is the most visited, by user 
    DT[, "MostVisited" := {counts <- table(cellxy); names(counts)[which(counts==max(counts))]}, by=user]

<小时/>

我不确定你想要计算相对于MostVisited的平均x,y的具体程度,但我确信data.table也可以相对直接。

    ## But perhaps something like this
    DT[, c("AvgX", "AvgY") := list(mean(x), mean(y)), by=list(user, MostVisited)]