基于三个分组变量 R 将单独的行聚集成一个

Gathering separate rows into one based on three grouping variables R

我正在为一些数据争论而苦恼。下面是一个最小的工作示例。我需要一些滚动循环,对于每个独特的 participant*item*cond 组合,从第一个开始看行到最后一行,将 thresh 中的每个值粘贴到一个新列中(应该看起来像 what_I_want)。有人有什么建议吗?

my_df <- data.frame("participant" = c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
                                "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2"), 
              "item" = c("101", "101", "101", "101", "101", "101", "101", "101", "102", "102", "102", "102", "102", "102", "102", "102", "101", "101", "101", "101", "101", "101", "101", "101", "103", "103", "103", "103", "103", "103", "103", "103"),
              "cond" = c("a", "a", "a", "a", "b", "b", "b", "b", "a", "a", "a", "a", "b", "b", "b", "b", "a", "a", "a", "a", "b", "b", "b", "b", "a", "a", "a", "a", "b", "b", "b", "b"),
              "thresh" = c(1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1),
              "what_I_want" = c("1011", "1011", "1011", "1011", "1101", "1101", "1101", "1101", "1001", "1001", "1001", "1001", "1111", "1111", "1111", "1111", "0011", "0011", "0011", "0011", "0000", "0000", "0000", "0000", "1010", "1010", "1010", "1010", "1011", "1011", "1011", "1011"))

您可以按三列(即participantitemcond)分组,然后使用paste0中的collapse组合所有thresh.

组中的值
library(tidyverse)

my_df %>% 
  group_by(participant, item, cond) %>% 
  mutate(result = paste0(thresh, collapse = ""))

基础 R 选项:

merge(my_df,
      setNames(
        aggregate(thresh ~ participant + item + cond, my_df, paste, collapse = ""),
        c("participant", "item", "cond", "result")
      ),
      by = c("participant", "item", "cond"))

输出

   participant item  cond  thresh what_I_want result
   <chr>       <chr> <chr>  <dbl> <chr>       <chr> 
 1 1           101   a          1 1011        1011  
 2 1           101   a          0 1011        1011  
 3 1           101   a          1 1011        1011  
 4 1           101   a          1 1011        1011  
 5 1           101   b          1 1101        1101  
 6 1           101   b          1 1101        1101  
 7 1           101   b          0 1101        1101  
 8 1           101   b          1 1101        1101  
 9 1           102   a          1 1001        1001  
10 1           102   a          0 1001        1001  
11 1           102   a          0 1001        1001  
12 1           102   a          1 1001        1001  
13 1           102   b          1 1111        1111  
14 1           102   b          1 1111        1111  
15 1           102   b          1 1111        1111  
16 1           102   b          1 1111        1111  
17 2           101   a          0 0011        0011  
18 2           101   a          0 0011        0011  
19 2           101   a          1 0011        0011  
20 2           101   a          1 0011        0011  
21 2           101   b          0 0000        0000  
22 2           101   b          0 0000        0000  
23 2           101   b          0 0000        0000  
24 2           101   b          0 0000        0000  
25 2           103   a          1 1010        1010  
26 2           103   a          0 1010        1010  
27 2           103   a          1 1010        1010  
28 2           103   a          0 1010        1010  
29 2           103   b          1 1011        1011  
30 2           103   b          0 1011        1011  
31 2           103   b          1 1011        1011  
32 2           103   b          1 1011        1011