我有一个数据框架,它包含两个列,Time和Response
df = cbind.data.frame(
Time = c(1, 1.2, 1.9, 2.2, 2.9, 3.1, 3.2, 3.2, 3.2, 3.6, 3.9, 4, 5.1, 5.99),
Response = c(1, 1, 1, 2, 3, 3, 3, 4, 3.5, 3.6, 3.3, 6, 11, 13)
)我想通过在同一分钟(时间)内将响应求和来转换它。[1-2]、[2-3)、[3-4]、[4-5]和5及以上。
预期的数据框架将是
dfe = cbind.data.frame(
time.range = c(1, 2, 3, 4, 5),
Response = c(3, 5, 19.4, 6, 24)
)发布于 2019-03-28 10:32:10
我们可以使用floor对其进行每分钟的分组。
library(dplyr)
df %>%
group_by(minute = floor(Time)) %>%
summarise(Response = sum(Response))
# minute Response
# <dbl> <dbl>
#1 1 3
#2 2 5
#3 3 20.4
#4 4 6
#5 5 24 在R基中使用aggregate
aggregate(Response~floor(Time), df, sum)也可以使用tapply
tapply(df$Response, floor(df$Time), sum)以及完成data.table选项
library(data.table)
setDT(df)[,sum(Response), by = floor(Time)]发布于 2019-03-28 17:02:03
我们可以使用来自rowsum的base R
rowsum(df$Response, as.integer(df$Time))
# [,1]
#1 3.0
#2 5.0
#3 20.4
#4 6.0
#5 24.0https://stackoverflow.com/questions/55395339
复制相似问题