0

我之前问过这个问题,得到的答复为我解决了这个问题。我有一个看起来像这样的数据框:

id                              weekdays              halflife
241732222300860000  Friday, Aug 31, 2012, 22    0.4166666667
241689170123309000  Friday, Aug 31, 2012, 19    0.3833333333
241686878137512000  Friday, Aug 31, 2012, 19    0.4
241651117396738000  Friday, Aug 31, 2012, 16    1.5666666667
241635163505820000  Friday, Aug 31, 2012, 15    0.95
241633401382265000  Friday, Aug 31, 2012, 15    2.3666666667

我想获得周一创建的物品的平均半衰期,然后是周二......等等。(我的日期范围跨越 6 个月)。

要获取我使用的日期值strptimedifftime. 另外,我用 找到了最大半衰期,max(df$halflife)我怎样才能找到它对应的 id?

可重现的代码:

structure(list(id = c(241732222300860416, 241689170123309056, 
241686878137511936, 241651117396738048, 241635163505819648, 241633401382264832
), weekdays = c("Friday, Aug 31, 2012, 22", "Friday, Aug 31, 2012, 19", 
"Friday, Aug 31, 2012, 19", "Friday, Aug 31, 2012, 16", "Friday, Aug 31, 2012, 15", 
"Friday, Aug 31, 2012, 15"), halflife = structure(c(0.416666666666667, 
0.383333333333333, 0.4, 1.56666666666667, 0.95, 2.36666666666667
), class = "difftime", units = "mins")), .Names = c("id", 
"weekdays", "halflife"), row.names = c(NA, 6L), class = "data.frame")

所以现在,我有一个平均半衰期值所有星期一,星期二......等。如何获得这些工作日内所有时间的平均值,即:所有星期一上午 9 点、上午 10 点、上午 11 点等创建的所有项目的平均半衰期。然后是周二上午 9 点、10 点、11 点……等等。weekdays 列中的日期经过格式化,以便逗号后的最后一个数字是它的创建时间。我对正则表达式和模式匹配真的很糟糕,这就是我问这个后续问题的原因。

4

1 回答 1

1

使用基本软件包,您可以执行以下操作。

> mydf
            id                 weekdays       halflife
1 2.417322e+17 Friday, Aug 31, 2012, 22 0.4166667 mins
2 2.416892e+17 Friday, Aug 31, 2012, 19 0.3833333 mins
3 2.416869e+17 Friday, Aug 31, 2012, 19 0.4000000 mins
4 2.416511e+17 Friday, Aug 31, 2012, 16 1.5666667 mins
5 2.416352e+17 Friday, Aug 31, 2012, 15 0.9500000 mins
6 2.416334e+17 Friday, Aug 31, 2012, 15 2.3666667 mins

我们可以不使用正则表达式,而是strsplit在 的每个元素上weekdays使用unlist结果,然后以 4 列格式返回,matrix然后cbind使用mydf.

> mydf2 <- cbind(mydf, matrix(unlist(sapply(mydf$weekdays, strsplit, split=',')), byrow=TRUE, ncol=4, dimnames=list(1:nrow(mydf), c('Weekday', 'Day', 'Year', 'Hour'))))
> mydf2
            id                 weekdays       halflife Weekday     Day  Year Hour
1 2.417322e+17 Friday, Aug 31, 2012, 22 0.4166667 mins  Friday  Aug 31  2012   22
2 2.416892e+17 Friday, Aug 31, 2012, 19 0.3833333 mins  Friday  Aug 31  2012   19
3 2.416869e+17 Friday, Aug 31, 2012, 19 0.4000000 mins  Friday  Aug 31  2012   19
4 2.416511e+17 Friday, Aug 31, 2012, 16 1.5666667 mins  Friday  Aug 31  2012   16
5 2.416352e+17 Friday, Aug 31, 2012, 15 0.9500000 mins  Friday  Aug 31  2012   15
6 2.416334e+17 Friday, Aug 31, 2012, 15 2.3666667 mins  Friday  Aug 31  2012   15

现在我们已经适当地拆分了工作日列,我们可以使用aggregate函数来计算mean所需的分组列。

> aggregate(halflife ~ Weekday, data=mydf2, FUN = mean)
  Weekday  halflife
1  Friday 1.013889 

如果你想分组WeekdayHour那么

> aggregate(halflife ~ Weekday + Hour, data=mydf2, FUN = mean)
  Weekday Hour   halflife
1  Friday   15 1.6583333 
2  Friday   16 1.5666667 
3  Friday   19 0.3916667 
4  Friday   22 0.4166667 

因此,aggregate这里函数的第一个参数是一个论坛对象,它支持一~一、一~多、多~一和多~多关系。查看?aggregate示例以了解如何使用它。

我将给出一个简单的例子来说明多对多的关系。

> set.seed(12345)
> mydf2 <- cbind(mydf2, newvar = rnorm(nrow(mydf2)))
> mydf2
            id                 weekdays       halflife Weekday     Day  Year Hour     newvar
1 2.417322e+17 Friday, Aug 31, 2012, 22 0.4166667 mins  Friday  Aug 31  2012   22  0.5855288
2 2.416892e+17 Friday, Aug 31, 2012, 19 0.3833333 mins  Friday  Aug 31  2012   19  0.7094660
3 2.416869e+17 Friday, Aug 31, 2012, 19 0.4000000 mins  Friday  Aug 31  2012   19 -0.1093033
4 2.416511e+17 Friday, Aug 31, 2012, 16 1.5666667 mins  Friday  Aug 31  2012   16 -0.4534972
5 2.416352e+17 Friday, Aug 31, 2012, 15 0.9500000 mins  Friday  Aug 31  2012   15  0.6058875
6 2.416334e+17 Friday, Aug 31, 2012, 15 2.3666667 mins  Friday  Aug 31  2012   15 -1.8179560
> aggregate(cbind(newvar,halflife) ~ Weekday + Hour, data=mydf2, FUN = mean)
  Weekday Hour     newvar  halflife
1  Friday   15 -0.6060343 1.6583333
2  Friday   16 -0.4534972 1.5666667
3  Friday   19  0.3000814 0.3916667
4  Friday   22  0.5855288 0.4166667
于 2013-02-21T11:21:18.243 回答