【问题标题】:How to summarise unique values in columns of a dataframe by multiple nested blocks in R如何通过R中的多个嵌套块汇总数据框列中的唯一值
【发布时间】:2019-10-07 21:24:34
【问题描述】:

我有一个数据框,其中包含日期时间、ID、时间和深度的列。我正在使用 ddply 来获取每个唯一日期时间的平均时间和深度,因为有重复的日期时间行。但是,在每个日期时间块中,可能有多个唯一的“id”值,并且对于单个 id 再次重复行。因此,我需要为每个 datetime 块做的实际上是计算从 id 块中获取的所有唯一时间的平均值。即我首先需要从每个 id 块中获取唯一时间值,然后我想计算使用此方法为每个 datetime 块返回的所有唯一时间值的平均值。我正在尝试使用 %>% 来执行此操作,但这对我来说是新语法,我正在苦苦挣扎。 ddply 包装器中针对日期时间的任何帮助或替代建议将不胜感激。我在下面提供一个示例。

> dput(df3)
structure(list(datetime = c("23/03/2017 14:13:45", "23/03/2017 14:13:45", 
"23/03/2017 14:13:45", "23/03/2017 14:13:45", "23/03/2017 14:13:45", 
"23/03/2017 14:13:45", "23/03/2017 14:13:45", "23/03/2017 14:13:45", 
"23/03/2017 14:13:45", "23/03/2017 14:13:45", "23/03/2017 14:15:15", 
"23/03/2017 14:15:15", "23/03/2017 14:15:15", "23/03/2017 14:15:15", 
"23/03/2017 14:15:45", "23/03/2017 14:15:45", "23/03/2017 14:16:15", 
"23/03/2017 14:16:15", "23/03/2017 14:16:15", "23/03/2017 14:16:15", 
"23/03/2017 14:16:15", "23/03/2017 14:16:15", "23/03/2017 14:16:15"
), id = c(11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 
12L, 12L, 13L, 14L, 14L, 15L, 16L, 16L, 16L, 17L, 18L, 18L), 
    time = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
    3L, 3L, 3L, 1L, 2L, 2L, 1L, 3L, 3L, 3L, 1L, 2L, 2L), dep = c(0.448675132, 
    0.448675132, 0.448675132, 0.448675132, 0.448675132, 0.448675132, 
    0.448675132, 0.448675132, 0.448675132, 0.448675132, 0.285520539, 
    0.285520539, 0.285520539, 0.285520539, 0.316112025, 0.316112025, 
    0.326309187, 0.356900674, 0.356900674, 0.356900674, 0.38749216, 
    0.326309187, 0.326309187)), class = "data.frame", row.names = c(NA, 
-23L))

我的尝试不起作用:

#convert datetime to POSIXct
df3$datetime = as.POSIXct(strptime(df3$datetime, format="%d/%m/%Y %H:%M:%S"), tz="UTC")

#Now condense the dateframe by unique datetime summarising tim and dep cols
  dfCondensed = ddply(df3, .(datetime), summarise,
                      #get the mean time for each unique datetime, but calculate this using 
                      #all the unique time values found within each unique id 
                      meantime = group_by(id) %>% unique(time) %>% mean(),
                      #do the same as above but for dep
                      meandep = group_by(id) %>% unique(dep) %>% mean())

期望的输出

> dfCondensed
             datetime   tim       dep
1 23/03/2017 14:13:45 10.00 0.4486751
2 23/03/2017 14:15:15  2.00 0.2855205
3 23/03/2017 14:15:45  2.00 0.3161120
4 23/03/2017 14:16:15  1.75 0.3492528

【问题讨论】:

    标签: r datetime nested plyr


    【解决方案1】:

    这是data.table 方法

    library(data.table)
    setDT(df3)
    unique(df3, by = c("datetime", "id"))[, .(mean.time = mean(time),
                                              mean.dep = mean(dep)), 
                                          by = .(datetime)][]
    
                  datetime mean.time  mean.dep
    1: 23/03/2017 14:13:45     10.00 0.4486751
    2: 23/03/2017 14:15:15      2.00 0.2855205
    3: 23/03/2017 14:15:45      2.00 0.3161120
    4: 23/03/2017 14:16:15      1.75 0.3492528
    

    【讨论】:

      【解决方案2】:

      我想你正在寻找:

      library(dplyr)
      
      df3 %>%
         distinct() %>%
         group_by(datetime) %>%
         summarise(dep = mean(dep), mean = mean(time))
      
      #  datetime              dep  mean
      #  <chr>               <dbl> <dbl>
      #1 23/03/2017 14:13:45 0.449 10   
      #2 23/03/2017 14:15:15 0.286  2   
      #3 23/03/2017 14:15:45 0.316  2   
      #4 23/03/2017 14:16:15 0.349  1.75
      

      【讨论】:

      • 谢谢,这确实给了我一个输出,但我需要的是代码来实现我想要在上面的 ddply 包装器中做的事情,因为我已经以不同的方式总结了一堆其他列在这个函数中。我只需要一行代码(或两行 - 一个用于同时,一个用于 meandep),它返回已应用于日期时间块的 ddply 包装器内的方法。因此,我不能在日期时间再次使用 group_id。
      • @jjulip plyrretired。你介意切换到dplyr 吗?你可以用它做所有的总结和其他任务。
      • 是的,我已经更新了。谢谢。但我仍然不明白如何按照我的要求进行这项工作。我遇到的问题(我不能在这里举一个例子)是我有一个很大的数据框,除了时间和深度之外,还有很多其他列,所以我认为当我输入 distinct() other 时你的代码正在做其他事情而不是使其特定于我的“id”分组,这是我需要的。请你告诉我如何解决这个问题?
      • @jjulip 我明白了,在这种情况下,您可以试试df3 %&gt;% distinct(datetime, id) %&gt;% group_by(datetime) %&gt;% summarise(dep = mean(dep), mean = mean(time)),它只会为您提供datetimeid 的唯一行。
      • 当我尝试这个时,它无法从 df3 中找到我的对象(即 dep)。我只是收到一条错误消息,告诉我“找不到对象'dep'”。
      【解决方案3】:

      我们可以使用base R

      df4 <- unique(df3)
      by(df4[c('time', 'dep')], df4[c('datetime')], FUN = colMeans)
      

      或与aggregate 来自base R

      aggregate(cbind(time, dep) ~ datetime, df4, mean)
      #     datetime  time       dep
      #1 23/03/2017 14:13:45 10.00 0.4486751
      #2 23/03/2017 14:15:15  2.00 0.2855205
      #3 23/03/2017 14:15:45  2.00 0.3161120
      #4 23/03/2017 14:16:15  1.75 0.3492528
      

      【讨论】:

      • 不幸的是,这对我没有帮助,因为我的数据框中有大量列,并且我想同时汇总来自多个列的数据,而不是简单地计算两个的平均值列,这就是为什么我将 ddply 函数用于日期时间块。我还需要从每个 id 块中获取唯一值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2015-02-11
      相关资源
      最近更新 更多