1

我有一个看起来像这样的数据框:

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 个月)。请让我知道如何提供可重现的代码,因为我无法找到附加文件的方法。

为了获取日期值,我使用了 strptime 和 difftime。另外,我用 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")
4

1 回答 1

4

可能有更好的方法来获取工作日,但您可以tapply像这样使用(这里df是您的数据框的名称):

days <- sub(",.*$", "", df$weekdays)
tapply(df$halflife, days, mean)

要获取最大值的 id,请使用which

df$id[which(df$halflife==max(df$halflife))]
于 2013-02-15T15:36:37.757 回答