【问题标题】:Creating Mean Function for Subset in R在 R 中为子集创建平均函数
【发布时间】:2016-01-14 13:51:44
【问题描述】:

我正在尝试创建一个函数,该函数将采用几个参数并返回总平均每小时回报。我的数据集如下所示:

Location    Time    units
1   Columbus    3:35    12
2   Columbus    3:58    199
3   Chicago     6:10    -45
4   Chicago     6:19    87
5   Detroit    12:05    -200
6   Detroit     0:32    11

我想要返回的是

Location    Time    units   unitsph
Columbus    7:33        211     27.9
Chicago     12:29       42      3.4
Detroit     12:37      -189    -15.1

同时还保留其他项目

基本上是生产的总单位数和每小时的单位数。

我试过了

thing <- time %>% group_by(Location) %>% summarize(sum(units))

返回位置和总单位,但不返回每小时单位。然后我搬到了

thing <- time %>% group_by(Location) %>% summarize(sum(units)) %>% summarize(sum(Time))

返回的

Error in eval(expr, envir, enclos) : object 'Time' not found

我也尝试过变异,但没有效果:

fin <- mutate(time, as.numeric(sum(Time))/as.numeric(sum(units)))
Error in Summary.factor(c(118L, 131L, 174L, 178L, 57L), na.rm = FALSE) : 
  ‘sum’ not meaningful for factors

非常感谢这里的任何帮助。我还有一些我想保留的其他列(它们是位置的地理编码等),但没有在此处列出。如果这很重要,我可以重新添加。

【问题讨论】:

  • Play.Time 不是 Time 对于初学者。另外,你如何总结 12:05 像时间对象?它们是如何存储的?
  • 啊,是的,编辑的乐趣。 Play.Time 是实际变量,为了简单起见,我在问题中设置了时间。编辑以解决该问题。时间项目被存储为因素。它们是由 lubridate 生成的,但老实说我不记得我是如何生成它们的(已经改进了一段时间了)。
  • 我想我的答案已经过时了。您可以将dput(time) 的输出添加到您的问题中,以使其更容易重现。此外,thing &lt;- time %&gt;% group_by(Location) %&gt;% summarize(sum(units)) %&gt;% summarize(sum(Time)) 行包含一个错误。请参阅下面的答案
  • dput(time) 获取:list(Location = structure(c(9L, 9L, 9L, 9L, 9L, 13L, 13L, 13L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), .Label = c("Columbus", "Detroit", "Chicago"), class= "factor"), Time = structure(c(118L, 131L, 174L, 178L, 57L, 8L, 91L, 108L, 66L, 91L, 96L, 105L, 129L, 141L, 145L, 180L, 190L, 207L, 208L), .Label = c("0:01", "0:05", "0 :06" ), class= "因子")
  • 注意:这里有很多列和行我删除了问题的实质。

标签: r dplyr


【解决方案1】:

你的时间是一个字符串对象。你可以使用

data <- data.frame(loc=c("C","C","D","D"),time=c("1:22","1:23","1:24","1:25"),u=c(1,2,3,4))
basetime <- strptime("00:00","%H:%M")
data$in.hours <- as.double(strptime(data$time,"%H:%M")-basetime)
thing <- data %>% group_by(loc) %>% summarize(sum(u),sum(in.hours))

转换为小时并不是很漂亮。它首先将时间转换为 Posix.ct 对象,然后再将其转换为双精度。不过猜对了。 转换后的数据

 loc time u in.hours
1   C 1:22 1 1.366667
2   C 1:23 2 1.383333
3   D 1:24 3 1.400000
4   D 1:25 4 1.416667

所以1.366 表示1h + 1/3h。 那么最终的结果就是

    loc sum(u) sum(in.hours)
  (fctr)  (dbl)         (dbl)
1      C      3      2.750000
2      D      7      2.816667

因此对于C,您有 2 小时,0.75*60 minutes

【讨论】:

  • 我对此很感兴趣:strptime 是做什么的?另外,我在这里打印了一个变量子集。总之,有一些 8k 记录。 (目标是将所有这些用于闪亮的仪表板、反应项目)。
  • @ike - ? 可以找到函数的作用 - 例如?strptime
  • 您好 thelatemail:感谢您的建议。我更好奇为什么这里的答案使用它。例如,我从 '?strptime' 知道 %H 将给定时间转换为小数。我对他们为什么在这里这样做而不是说要转换成一个整体很感兴趣。
【解决方案2】:

我最终接受了@CAFEBABE 推荐的部分内容并对其进行了修改。

我用过

mutated_time <- time %>% 
    group_by(Location) %>% 
    summarize(play 
    = sum(as.numeric(Time)/60),
    unitsph = sum(units))

还有那个加分

selektor <- as.data.frame(select(distinct(mutated_time), Location,unitsph))

把我带到了我想去的地方。感谢大家提供许多有用的 cmets。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 2015-05-29
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多