1
votes

I have speech data in utterance and gaze data in columns A_aoi, B_aoi, and C_aoi. Some of the utterancerows are duplicated:

df <- data.frame(
  line = c(1,2,3,4,4,4,5,6,6,7,8),
  speaker = c("b", "a", NA, "c", "c", "c", NA, "c", "c", "a", "a"),
  utterance = c("Hey sweetheart!", "Louise!", "(0.234)", "What?", "What?", "What?", "(0.778)", "um::", "um::", "Wake up,", "breakfast's ready"),
  A_aoi = c("B", "B", "C", "B", NA, "C", "C", NA, "C", "C", "C"),
  B_aoi = c("C", "C", "C", "C", "A", "C", NA, NA, "C", "C", NA),
  C_aoi = c("A", NA, NA, "B", NA, "C", "C", "A", "A", "A", "A")
)

What I need to do is collapse the rows by rleid groups. However, where utterance is duplicated, there should be no collapse.

I'm fine with collapsing the rows by rleid groups:

library(dplyr)
library(data.table)
library(stringr)
df %>%
  group_by(grp = rleid(speaker)) %>% 
  summarise(across(c(line, speaker), first), 
            utterance = str_c(utterance, collapse = ' '), 
            A_aoi = str_c(if_else(!is.na(A_aoi), A_aoi, "*" ), collapse = ""), 
            B_aoi = str_c(if_else(!is.na(B_aoi), B_aoi, "*" ), collapse = ""),
            C_aoi = str_c(if_else(!is.na(C_aoi), C_aoi, "*" ), collapse = ""), .groups = 'drop') %>%
  select(- grp)

This however also collapses the duplicated utterance values. The expected result is this:

# A tibble: 7 x 6
   line speaker utterance                  A_aoi B_aoi C_aoi
  <dbl> <chr>   <chr>                      <chr> <chr> <chr>
1     1 b       Hey sweetheart!            B     C     A    
2     2 a       Louise!                    B     C     *    
3     3 NA      (0.234)                    C     C     *    
4     4 c       What?                      B*C   CAC   B*C  
5     5 NA      (0.778)                    C     *     C    
6     6 c       um::                       *C    *C    AA   
7     7 a       Wake up, breakfast's ready CC    C*    AA 

Any help is appreciated!

EDIT:

I have a stepwise solution but if anybody has a better, less convoluted one I'll be most grateful:

# step 1 -- collapse only `aoi` columns:
df_a <- df %>%
  group_by(grp = rleid(speaker)) %>% 
  summarise(across(c(line, speaker), first),  
            A_aoi = str_c(if_else(!is.na(A_aoi), A_aoi, "*" ), collapse = ""), 
            B_aoi = str_c(if_else(!is.na(B_aoi), B_aoi, "*" ), collapse = ""),
            C_aoi = str_c(if_else(!is.na(C_aoi), C_aoi, "*" ), collapse = ""), .groups = 'drop') %>%
  select(- c(grp, line, speaker))

# step 2 -- remove duplicates:
df_b <- df[-which(duplicated(df$line)),]

# step 3 -- collapse `utterance`:
df_c <- df_b %>%
  group_by(grp = rleid(speaker)) %>% 
  summarise(across(c(line, speaker), first), 
            utterance = str_c(utterance, collapse = ' '), .groups = 'drop') %>%
  select(- grp)

# step 4 -- bind:
bind_cols(df_c, df_a)
1

1 Answers

2
votes

What about using unique(utterance)? Would this help you achieve what you want?

df %>%
  group_by(grp = rleid(speaker)) %>% 
  summarise(across(c(line, speaker), first), 
    utterance = str_c(unique(utterance), collapse = ' '), 
    A_aoi = str_c(if_else(!is.na(A_aoi), A_aoi, "*" ), collapse = ""), 
    B_aoi = str_c(if_else(!is.na(B_aoi), B_aoi, "*" ), collapse = ""),
    C_aoi = str_c(if_else(!is.na(C_aoi), C_aoi, "*" ), collapse = ""), .groups = 'drop') %>%
  select(- grp)

Output

# A tibble: 7 x 6
   line speaker utterance                  A_aoi B_aoi C_aoi
  <dbl> <chr>   <chr>                      <chr> <chr> <chr>
1     1 b       Hey sweetheart!            B     C     A    
2     2 a       Louise!                    B     C     *    
3     3 NA      (0.234)                    C     C     *    
4     4 c       What?                      B*C   CAC   B*C  
5     5 NA      (0.778)                    C     *     C    
6     6 c       um::                       *C    *C    AA   
7     7 a       Wake up, breakfast's ready CC    C*    AA