使用ddply总结我已经定义了“平均”的功能,并用它在ddply我R中
数据:使用ddply总结我已经定义了“平均”的功能,并用它在ddply我R中
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))
正如你所看到的,平均函数使用“切割”的时间间隔,然后查找平均。我想在我的输出中显示间隔。目前我得到以下输出:
> 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]
我不知道如何在“平均”功能或ddply
命令指定。请帮助
通常,将命令捆绑到一个函数中的一点是,您不必担心中间步骤。你已经做到了,但现在你也需要中间结果(你的“间隔”)。我认为唯一好的解决方案是将你的功能分开。
定义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
谢谢,但这不适合我。我的“平均”功能的目的是在定义的时间间隔内获得均值。你的代码计算所有值的均值 –
@umairdurrani我纠正了一个错误(我有“速度”,我应该有“速度”),但代码确实有效。在“ddply”的第二个参数中,它指定'“interval”'作为分组变量。因此,将针对区间的每个唯一值计算均值。 – Gregor
非常感谢@shujaa。 –
这是非常容易的帮助,如果你提供一个[最小的,可重复的例子(http://stackoverflow.com/questions/5963269/how-to- make-a-great-r-reproducible-example/5963610#5963610) – Henrik
@Henrik现在可以理解吗? –