首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中计算重复测量/长数据的汇总统计数据

问在R中计算重复测量/长数据的汇总统计数据
EN

Stack Overflow用户
提问于 2021-06-10 22:16:41
回答 1查看 43关注 0票数 0

如果我在其他地方问过这个问题/如果我使用了错误的术语,很抱歉,我一直在努力寻找正确的方法来做到这一点,但到目前为止没有成功。

我有一个实验设计,有3个实验条件,使用重复测量的结果(每个参与者完成4个试验)。我目前拥有的数据是长格式的(每个参与者ID重复4次)。我正在尝试计算人口统计变量(年龄、性别、病情等)的汇总统计数据。但是,由于缺乏更好的词,我不知道如何将每个参与者的行折叠/合并在一起,以获得频率数据和/或汇总统计数据。

下面我有一个模拟的数据集

代码语言:javascript
复制
require(tidyverse)
require(summarytools)
require(skimr)
require(lme4)
require(wakefield) #to simulate age distribution
require(reshape2)

id <- rep(1:150, each = 4)
age <- rep(age(150, x = 18:21), each = 4)
gender <- rep(c("male", "male", "male", "male", "female", "female","female","female"), each = 25, times = 3)
condition <- rep(c("condition_1", "condition_2", "condition_3"), each = 4, times = 50) #condition
control_1 <- rep(c("order_1", "order_2"), each = 4, length.out = 600) # control variable for counterbalancing
control_2 <- rep(c("group_1", "group_2"), each = 75, length.out = 600) control variable for counterbalancing
test1_trial <- rep(c("trial_1", "trial_2", "trial_3", "trial_4"), each = 1, length.out = 600)
test1_outcome <- rbinom(600, 1, 0.5) # actual data
test2_trial <- rep(c("trial_1", "trial_2", "trial_3", "trial_4"), each = 1, length.out = 600)
test2_outcome <- rbinom(600, 1, 0.5) # actual data

dat <- data.frame(id, age, gender, condition, control_1, control_2, test1_trial, test1_outcome, test2_trial, test2_outcome)

我试过这样使用group_by

代码语言:javascript
复制
dat %>% 
  group_by(id) %>% 
  freq(age)

但这使我将每个id作为一个单独的组,这对汇总统计信息显然没有帮助。

我也尝试过使用summarise_all,但无法正常工作

代码语言:javascript
复制
dat$id <- as.factor(dat$id)

dat %>% 
  select(id, age)
  group_by(id) %>% 
  summarise_all(funs(sum))

Error in UseMethod("group_by") : no applicable method for 'group_by' applied to an object of class "c('integer', 'numeric')"

对于汇总统计数据,我不关心实际数据(即test1_outcome和test2_outcome),我只希望能够计算平均年龄,每个条件的参与者数量等。有没有一种方法可以只选择控制/人口统计变量,并为每个参与者折叠它们?

对于基本问题,我很抱歉,我通常不使用重复测量设计,因此不太熟悉长格式数据。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-11 04:38:00

如果您的人口统计数据在不同的治疗轮次之间没有变化,您可以只按id运行distinct()或unique(),类似于Jon Spring建议的,如下所示:

代码语言:javascript
复制
dat %>% 
distinct(id, age, gender)

然后,您可以按条件折叠,以获得此变量或任何其他变量的汇总统计数据,以及参与者数量:

代码语言:javascript
复制
dat %>% 
distinct(id, age, gender, condition) %>% 
group_by(condition, gender) %>% 
mutate(n = n()) %>% 
summarise_all( .funs = c(mean))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67922980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档