我想找到每月和每年电池运行分钟的总和。为此,我有以下条件:
如果Battery.voltage< 50然后"是的,否则"否。
注意:为了计算分钟的总和,我们可以在时间戳列中显示日,月,年,小时,分钟。
这是我的数据:
# Time.stamp Battery.voltage Condition
# 1 01/04/2016 00:00 51 No
# 2 01/04/2016 00:01 52 No
# 3 01/04/2016 00:02 45 Yes
# 4 01/04/2016 00:03 48 Yes
# 5 01/04/2016 00:04 49 Yes
# 6 01/04/2016 00:05 55 No
# 7 01/04/2016 00:06 54 No
# ...
structure(list(
Time.stamp = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 10L, 11L, 12L, 12L, 13L),
.Label = c("01/04/2016 00:00", "01/04/2016 00:01", "01/04/2016 00:02", "01/04/2016 00:03",
"01/04/2016 00:04", "01/04/2016 00:05", "01/04/2016 00:06", "01/04/2016 00:07",
"01/04/2016 00:08", "01/04/2016 00:09", "01/04/2016 00:11", "01/04/2016 00:12",
"01/04/2016 00:13"), class = "factor"),
Battery.voltage = c(51L, 52L, 45L, 48L, 49L, 55L, 54L, 52L, 51L, 49L, 48L, 47L, 45L, 50L, 51L),
Condition = structure(c(1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L),
.Label = c("No", "Yes"), class = "factor")),
.Names = c("Time.stamp", "Battery.voltage", "Condition"),
class = "data.frame", row.names = c(NA, -15L))
我的预期输出是这样的:
Month year Sum of mins running in battery
Jan 2016 350min
Feb 2016 450min
etc.
答案 0 :(得分:0)
很遗憾,您的示例数据不能很好地代表您的问题陈述,因为它只包含一天的数据。提供一些为足够的条目(即日期)生成随机数据的代码将是有益的。
除此之外,您可以调整以下解决方案(此处我假设您的时间戳格式为"DD/MM/YYYY"
):
df %>%
mutate(
Time.stamp = as.POSIXct(Time.stamp, format = "%d/%m/%Y %H:%M"),
byday = format(Time.stamp, "%d/%m/%Y"),
bymonth = format(Time.stamp, "%d/%m"),
byyear = format(Time.stamp, "%Y")) %>%
group_by(byday) %>%
summarise(sum.running.in.mins = sum(Condition == "Yes"))
## A tibble: 1 x 2
# byday sum.running.in.mins
# <chr> <int>
#1 01/04/2016 7
我们在此处创建列byday
,bymonth
和byyear
,您可以根据这些列对条目进行分组并计算每个组的总运行时间总和。在上面的例子中,我按天计算总运行时间;为了获得每月的总运行时间,您可以将group_by(byday)
替换为group_by(bymonth)
。