使用ddply汇总R中的数据

时间:2014-02-25 21:29:30

标签: r plyr

我已经定义了一个'average'函数并在ddply中使用它:

average <- function (parameter,speed) {
  sequence = seq(min(speed), max(speed), by=4.5)
  interval = cut(speed, sequence)
  avg = tapply(parameter, interval, mean)
  avg
}


df <- ddply(data1, c(unique('class'),unique('PrecVehClass')), summarise,avg.spacing=average(spacing,velocity),avg.headway=average(headway,velocity),avg.speed=average(velocity,velocity))

如您所见,平均函数使用“cut”创建间隔,然后找到平均值。我想在输出中也显示间隔。目前我得到以下输出:

> head(df)
  class PrecVehClass avg.spacing avg.headway avg.speed
1     1            1      129.10        2.50     51.80
2     1            1       91.80        1.62     56.79
3     1            2       25.65     6744.06      2.55
4     1            2       31.86       45.23      7.18
5     1            2       35.43        3.25     11.63
6     1            2       38.45        2.85     16.21

如何在每一行中添加一个显示间隔(即最小值和最大值,例如[31.8,36.2])的新列?

修改

以下是我数据集的前6行:

> dput(head(data1))
structure(list(vehicle = c(2L, 2L, 2L, 2L, 2L, 2L), frame = 43:48, 
    globalx = c(6451214.156, 6451216.824, 6451219.616, 6451222.548, 
    6451225.462, 6451228.376), class = c(2L, 2L, 2L, 2L, 2L, 
    2L), velocity = c(37.76, 37.9, 38.05, 38.18, 38.32, 38.44
    ), acceleration = c(10.44, 9.3, 4.36, -0.73, -1.15, 1.9), 
    lane = c(2L, 2L, 2L, 2L, 2L, 2L), precedingveh = c(0L, 0L, 
    0L, 0L, 0L, 0L), followingveh = c(13L, 13L, 13L, 13L, 13L, 
    13L), spacing = c(0, 0, 0, 0, 0, 0), headway = c(0, 0, 0, 
    0, 0, 0), u = c("no", "no", "no", "no", "no", "no"), PrecVehClass = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    )), .Names = c("vehicle", "frame", "globalx", "class", "velocity", 
"acceleration", "lane", "precedingveh", "followingveh", "spacing", 
"headway", "u", "PrecVehClass"), row.names = c(NA, 6L), class = "data.frame")

您可以看到我在上面定义的平均功能。除了输出中的平均值之外,我还想添加一个新列,显示找到平均值的“间隔”。如果我不使用ddply但使用tapply,比如avg.spacing,我会得到以下输出:

p <- tapply(data1$spacing, cut(data1$velocity, seq(min(data1$velocity), max(data1$velocity), by=4.5), mean)

> p
  (0,4.5]   (4.5,9]  (9,13.5] (13.5,18] (18,22.5] (22.5,27] (27,31.5] (31.5,36] (36,40.5] (40.5,45] (45,49.5] (49.5,54] 
 29.52244  37.44980  44.09410  50.19250  56.89366  61.90450  67.21415  72.83281  79.73360  88.38050  96.87901 105.47172 
(54,58.5] (58.5,63] (63,67.5] (67.5,72] (72,76.5] (76.5,81] (81,85.5] (85.5,90] 
116.13763 120.46700 126.49401 136.43546 174.28593 271.90232 255.20733        NA 

在上面的输出中,您可以看到报告的间隔以及该间隔中间距的平均值。我想在我的最终表中得到这个间隔输出,如下所示:

 > head(df)
      class PrecVehClass avg.spacing avg.headway avg.speed  interval
    1     1            1      129.10        2.50     51.80  (0,4.5]
    2     1            1       91.80        1.62     56.79  (4.5,9]
    3     1            2       25.65     6744.06      2.55  (0,4.5]
    4     1            2       31.86       45.23      7.18  (4.5,9]
    5     1            2       35.43        3.25     11.63  (9,13.5]
    6     1            2       38.45        2.85     16.21  (13.5,18]

我不知道如何在'average'函数OR ddply命令中指定它。请帮忙

1 个答案:

答案 0 :(得分:1)

通常,将命令捆绑到一个函数中是这样的,因此您不必担心中间步骤。你已经完成了,但现在你也想要中间结果(你的“间隔”)。我认为唯一的好办法是将你的功能分开。

首先定义interval,您可以在ddply中将其用作分组变量,并使用普通的mean,除非我误解了average的目的功能

df$interval <- with(df, cut(velocity, seq(min(velocity), max(velocity), by = 4.5)))
df <- ddply(df, c("class", "PrecVehClass", "interval"), summarise,
            avg.spacing = mean(spacing),
            avg.headway = mean(headway),
            avg.speed = mean(velocity))

另请注意ddply中的分组变量,您不需要unique()包装器。


ddply示例:

df1 <- data.frame(x = rnorm(100))
df1$interval <- cut(df1$x, breaks=c(-10, -1, 1, 10))
ddply(df1, "interval", summarize, mean_within_interval = mean(x))
  interval mean_within_interval
1 (-10,-1]           -1.5262258
2   (-1,1]            0.0880585
3   (1,10]            1.4796220