【问题标题】:Is there a way to loop through data based on factor in a column and add up the number of rows?有没有办法根据列中的因子循环数据并加起来行数?
【发布时间】:2019-08-28 23:41:02
【问题描述】:

我有一些数据,其中我对同一事件进行了多次观察。基于时间阈值,我想浓缩观察结果。但我想知道我正在浓缩多少(即有多少观察成为一个)。我不确定如何以这种方式遍历我的数据框。

我尝试过编写 for 循环、if 语句、while 语句,并且在 google 和堆栈溢出上不知疲倦地搜索。似乎没有什么与我需要做的事情有关。

这是我的数据的一个子集:

structure(list(date.time = structure(c(1465877617, 1465877774, 
1465877816, 1465877844, 1465912214, 1465912806, 1465912862, 1465914033
), class = c("POSIXct", "POSIXt"), tzone = "America/New_York"), 
    time = structure(1:8, .Label = c("00:13:37", "00:16:14", 
    "00:16:56", "00:17:24", "09:50:14", "10:00:06", "10:01:02", 
    "10:20:33"), class = "factor"), X = c(1, 1, 1, 1, 1, 1, 1, 
    1), diff_time1 = structure(c(157, 42, 28, 34370, 592, 56, 
    1171, 2820), class = "difftime", units = "secs"), diff_time2 = c(FALSE, 
    FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE), new = c("start", 
    "include", "include", "end", "start", "include", "end", "start-end"
    )), row.names = c(NA, 8L), class = "data.frame")

我们的目标是让它看起来像下面,但每个“模糊”的观察都有一个额外的样本大小列:

structure(list(n = 1:8, end = structure(c(1465877844, 1465912862, 
1465914033, 1465916853, 1465921999, 1465928992, 1465933159, 1465937668
), class = c("POSIXct", "POSIXt")), start = structure(c(1465877617, 
1465912214, 1465914033, 1465916853, 1465921999, 1465928647, 1465932867, 
1465937418), class = c("POSIXct", "POSIXt")), date = structure(c(16966, 
16966, 16966, 16966, 16966, 16966, 16966, 16966), class = "Date")), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame"))

【问题讨论】:

    标签: r loops data-manipulation


    【解决方案1】:
    library(dplyr); library(lubridate)
    df %>%
      mutate(time_since_last = (date.time - lag(date.time, default = first(date.time))) / dminutes(1)) %>%
      mutate(group = 1 + cumsum(time_since_last > 15)) %>% # How many times was there a 15min+ gap? Each new one increments "group"
      group_by(group) %>%
      summarize(first = min(date.time), # or first(date.time) if sorted
                last  = max(date.time), # or last(date.time) if sorted
                count = n())
    
    ## A tibble: 3 x 4
    #  group first               last                count
    #  <dbl> <dttm>              <dttm>              <int>
    #1     1 2016-06-14 00:13:37 2016-06-14 00:17:24     4
    #2     2 2016-06-14 09:50:14 2016-06-14 10:01:02     3
    #3     3 2016-06-14 10:20:33 2016-06-14 10:20:33     1
    

    【讨论】:

    • 这正是我所需要的!谢谢你,乔恩·斯普林!由于我是编码新手,而且学习量很大,你是怎么想出来的?
    • 我衷心推荐r4ds.had.co.nz 这本书来介绍dplyr 和相关软件包。我很喜欢他们如何立即做很多有用的事情。
    • 感谢您的参考。这看起来非常非常有用。
    • 快速问题 - 我仍然不清楚为什么您需要第二行中的 cumsum() 函数。一直没能破解原因。
    • 我想将每个连续的事件集分配给一个组。我定义“新组”的方式是“有 15 分钟或更长时间的休息时间吗?” cumsum 行计算到该点为止在行中发生了多少累积次数,因此 group = 1 中的所有内容都在之前发生过,group = 2 中的所有内容都在之后发生过一次,group = 3 中的所有内容都是之后发生了两次,等等(我们也可以省略 = 1 + 部分,但我喜欢让第一组为 1 而不是被称为组 0。)
    猜你喜欢
    • 2019-07-12
    • 2021-03-26
    • 1970-01-01
    • 2021-09-22
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多