R为每个参与者的条件选择n行

时间:2018-08-14 14:51:20

标签: r filter dplyr

我有关于多个单项研究的数据框架。对三名参与者(ID_AB2)进行了行为干预,并收集了基线和干预条件的数据(PhaseAB2:A =基线; B =干预)。最后,“ Occassions”是一个包含会话号的变量。

我想要做的是编写代码以获取每个参与者每个阶段的最后三个值,然后计算平均值。

 RateAB2 <- c(8, 4, 5, 5, 5, 4, 8, 7, 8, 5, 4, 8, 7, 4, 7, 7, 4, 3, 4, 4, 4, 5, 9, 6, 16, 13, 8, 9, 15, 17, 9, 10, 7, 3, 2, 4, 3, 3, 3, 7, 13, 18, 12, 18, 14, 17, 19, 15) %>% as.numeric()
 PhaseAB2 <- rep(c("A", "B"), each = 8, len = 48)
 OccasionsAB2 <- rep(1:16, len = 48)
 ID_AB2 <- rep(c("C1", "C2", "C3"), each = 16)
 db5 <- data.frame(ID_AB2, OccasionsAB2, PhaseAB2, RateAB2)

我当时想使用dbdplyr::filter(OccasionsAB2 == ...),但是代码将严格依赖于每个特定的数据集,并且无法为具有不同观察值的参与者选择不同的位置。

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

library(dplyr)

RateAB2 <- c(8, 4, 5, 5, 5, 4, 8, 7, 8, 5, 4, 
             8, 7, 4, 7, 7, 4, 3, 4, 4, 4, 5, 
             9, 6, 16, 13, 8, 9, 15, 17, 9, 
             10, 7, 3, 2, 4, 3, 3, 3, 7, 13, 
             18, 12, 18, 14, 17, 19, 15) %>% 
                as.numeric()
PhaseAB2 <- rep(c("A", "B"), each = 8, len = 48)
OccasionsAB2 <- rep(1:16, len = 48)
ID_AB2 <- rep(c("C1", "C2", "C3"), each = 16)
db5 <- data.frame(ID_AB2, OccasionsAB2, PhaseAB2, RateAB2)

db5 %>%
  group_by(ID_AB2, PhaseAB2) %>%       # for each ID and Phase
  top_n(3, OccasionsAB2) %>%           # keep last 3 occasions
  summarise(MEAN = mean(RateAB2)) %>%  # get the average
  ungroup()                            # forget the grouping

# # A tibble: 6 x 3
#   ID_AB2 PhaseAB2  MEAN
#   <fct>  <fct>    <dbl>
# 1 C1     A         6.33
# 2 C1     B         6   
# 3 C2     A         6.67
# 4 C2     B        12   
# 5 C3     A         4.33
# 6 C3     B        17 

您可以将summarise的过去更新为

summarise(MEAN = mean(RateAB2), OccasionSequence = paste0(OccasionsAB2, collapse = ","))

还可以获取每种情况下使用的场合的ID(作为字符串)。