2つの列を含むデータフレームがあり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)
)
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
aggregate
ベースRでの使用
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)]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加