我在r或任何编码语言方面都很新手。我做了一个实验,测试了铜对滴灌器堵塞的影响。我有滴头类型“铜”和“正常”,每周一次,我衡量他们是否在工作(1) (0)。所有滴头都开始工作(1),到实验结束时,所有滴头都被堵塞(0)。我还怀疑,某些类型的碎片可能阻止滴头,因为他们接近水源,所以位置(横向,位置)柱是重要的。数据样本:
structure(list(Date = structure(c(1660089600, 1660089600, 1660521600,
1660521600, 1660780800, 1660780800, 1661385600, 1661385600, 1661904000,
1661904000, 1662249600, 1662249600, 1662336000, 1662336000), tzone =
"UTC", class = c("POSIXct",
"POSIXt")), Lateral = structure(c(2L, 4L, 2L, 4L, 2L, 4L, 2L,
4L, 2L, 4L, 2L, 4L, 2L, 4L), levels = c("1", "2", "3", "4"), class =
"factor"),
Position = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), levels = c("1", "2", "3", "4", "5",
"6"), class = "factor"), `Distance (m)` = c(0.9, 1.8, 0.9,
1.8, 0.9, 1.8, 0.9, 1.8, 0.9, 1.8, 0.9, 1.8, 0.9, 1.8), Type =
structure(c(2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), levels =
c("Copper",
"Normal"), class = "factor"), Working = c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L), id = c(6L, 18L,
6L, 18L, 6L, 18L, 6L, 18L, 6L, 18L, 6L, 18L, 6L, 18L)), row.names =
c(NA,
-14L), class = c("tbl_df", "tbl", "data.frame"))我需要计算多少时间(在几天内),它需要一个特定的滴头被阻塞(从1切换到0),并保持阻塞到实验结束。如果任何一个滴头被阻塞,但下面的测量再次起作用,那么忽略前一个0(也许标记这个滴头)。我想,我需要将工作列乘以从每个滴头开始的时间差,如果它等于零,保持前一个日期值,否则保持当前值。但是我不知道怎么写。任何帮助都将不胜感激!
发布于 2022-10-06 10:57:55
我编写了一个示例dataset,因为您没有提供一个可重复的示例,而且我使用的是{tidyverse}包和代码样式。
我假设你有多个滴头,每个滴头都有自己的id变量,我也假设每个滴头每天测量一次,所以你可以通过从第一次约会中减去它工作的最后一个日期来计算它的工作时间。
基本上,您会发现滴头连续两天的外观没有工作,从那时起丢弃该滴头的所有数据,并计算滴头工作的最后一次日期和第一次日期之间的差异:
library(tidyverse)
# dripper 'a' is the simple case, it works for 4 days and stops after;
# dripper 'b' works for 3 days, stops on day 4, and continues for another 2 after, then stops for good
drippers_example <- tibble(
id = rep(c("a","b"), each = 10),
date = seq.Date(from = as.Date("2022-08-18"), by = 1, length.out = 20),
working = c(rep(1,4),rep(0,6), c(1,1,1,0,1,1,0,0,0,0))
)
# here you compute when the drippers stopped working
drippers_processed <-
drippers_example %>%
group_by(id) %>% # process each dripper separately
mutate(
nonworking_consecutive_days =
# TRUE when current day is non-working and previous day was non-working also
working==0 & lag(as.integer(working), n = 1, default = 1)==0,
# set to TRUE after first consecutive non-working days
stopped_working = cumsum(nonworking_consecutive_days) > 0
) %>%
ungroup
dripper_summary <-
drippers_processed %>%
group_by(id) %>% # summarise each dripper separately
filter(!stopped_working) %>% # keep only entries before they stopped working
summarise(
# compute difference between the end date and the start date - that's how long it worked for
days_worked = as.numeric(max(date)-min(date), units = "days"),
.groups = "drop"
)如果您检查dripper_summary,您应该得到如下所需的示例数据:
> dripper_summary
# A tibble: 2 x 2
id days_worked
<chr> <dbl>
1 a 4
2 b 6https://stackoverflow.com/questions/73972220
复制相似问题