r - Average the periods 1-3 and 4-6 for each treatment and id for each variable and get the data in a new data frame
问题描述
Here I would like to average the periods 1-3 and 4-6 for each treatment and id for each variable and get the data in a new data frame. Does anyone know how I accomplish this?
set.seed(1)
id <- rep(1:2,each=6)
trt <- c("A","A","A","B", "B", "B","A","A","A","B", "B", "B")
period <- rep(1:6,2)
pointA <- sample(1:10,12, replace=TRUE)
pointB<- sample(1:10,12, replace=TRUE)
pointC<- sample(1:10,12, replace=TRUE)
df <- data.frame(id,trt,period,pointA, pointB,pointC)
head(df)
id trt period pointA pointB pointC
1 1 A 1 3 7 3
2 1 A 2 4 4 4
3 1 A 3 6 8 1
4 1 B 4 10 5 4
5 1 B 5 3 8 9
6 1 B 6 9 10 4
7 2 A 1 10 4 5
8 2 A 2 7 8 6
9 2 A 3 7 10 5
10 2 B 4 1 3 2
11 2 B 5 3 7 9
12 2 B 6 2 2 7
I would like it to look like this:
id trt Period pointA pointB pointC
1 1 A 123 13 19 8
2 1 B 456 21 23 17
3 2 A 456 24 22 16
4 2 B 123 6 12 18
解决方案
使用dplyr
您可以使用适当的组创建一个新变量,然后将其用作 group_by。例如
library(dplyr)
df %>%
mutate(period_class = case_when(
period %in% c(1,2,3)~"123",
period %in% c(4,5,6)~"456")
) %>%
select(-period) %>%
group_by(id, trt, period_class) %>%
summarize_all(mean) # though you seem to have used `sum` in your example
推荐阅读
- html - 如何使用 CSS 变换在抛物线路径 ie.curved 中移动图片?
- java - KeyCloak WildFly 独立模块 MySQL
- php - 有没有办法在 Laravel 7 中访问远程关系?
- javascript - 如何使用 Javascript 根据日期值过滤 JSON 数据
- javascript - 似乎无法从对象数组中访问单个对象
- php - 尝试上传 .doc 文件,但我总是收到请求超时
- javascript - Jquery一次只选择一个表行的单选输入并用复选标记替换
- javascript - 异步实际上不是异步
- javascript - 我的变量在 javascript 和 ajax 中失去了它的价值
- ibm-mq - 如何安装 WebSphere MQ 服务器?