Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

conditional statements - Merge strings and timestamps based on condition in R

I have transcriptions of speech with timestamps:

df
   line speaker                                utterance                   timestamp
1  0001  ID16.1                                    ah-ha 00:00:07.060 - 00:00:07.660
3  0002    <NA>                                      yes 00:00:07.964 - 00:00:08.610
5  0003    <NA> okay so where do we know each other from 00:00:16.350 - 00:00:22.170
7  0004  ID16.2        U uh Upper Rhine Cruises? maybe?  00:00:23.400 - 00:00:26.600
9  0005  ID16.3           yeah? ((pause)) well I do n't- 00:00:26.305 - 00:00:28.210
11 0006  ID16.1                               (...) Meg? 00:00:27.385 - 00:00:29.305
13 0007    <NA>                         do you know Meg? 00:00:29.100 - 00:00:33.879

What I need to do are two things: if speaker is NA, (i) append the string in column utterance to the utterance in the prior row, and (ii) merge the two timestamps accordingly.

The desired outcome is this:

df
   line speaker                                          utterance                   timestamp
1  0001  ID16.1 ah-ha yes okay so where do we know each other from 00:00:07.060 - 00:00:22.170
3  0004  ID16.2                  U uh Upper Rhine Cruises? maybe?  00:00:23.400 - 00:00:26.600
5  0005  ID16.3                     yeah? ((pause)) well I do n't- 00:00:26.305 - 00:00:28.210
7  0006  ID16.1                        (...) Meg? do you know Meg? 00:00:27.385 - 00:00:33.879

I've been trying to solve the problem using paste0, dplyr::lag, and dplyr:lead but have not come far.

Reproducible data:

df <- structure(list(line = c("0001", "0002", "0003", "0004", "0005", 
                    "0006", "0007"), speaker = c("ID16.1", NA, NA, "ID16.2", 
                                                 "ID16.3", "ID16.1", NA), utterance = c("ah-ha", "yes", 
                                                                                              "okay so where do we know each other from", 
                                                                                              "U uh Upper Rhine Cruises? maybe? ", "yeah? ((pause)) well I do n't-", 
                                                                                              "(...) Meg?", "do you know Meg?"
                                                 ), timestamp = c("00:00:07.060 - 00:00:07.660", "00:00:07.964 - 00:00:08.610", 
                                                                  "00:00:16.350 - 00:00:22.170", "00:00:23.400 - 00:00:26.600", 
                                                                  "00:00:26.305 - 00:00:28.210", "00:00:27.385 - 00:00:29.305", 
                                                                  "00:00:29.100 - 00:00:33.879")), row.names = c(1L, 3L, 5L, 7L, 
                                                                                                                 9L, 11L, 13L), class = "data.frame")
question from:https://stackoverflow.com/questions/65888735/merge-strings-and-timestamps-based-on-condition-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try dplyr::group_by. FYI, your displayed data is different from your df, which changes the aggregation.

library(dplyr)
df %>%
  group_by(notna = cumsum(!is.na(speaker))) %>%
  summarize(
    line = first(line), 
    speaker = first(speaker), 
    utterance = paste(utterance, collapse = " "), 
    timestamp = paste(unlist(strsplit(timestamp, "[- ]+"))[c(1, n()*2)], collapse = " - "),
    .groups = "drop"
  ) %>%
  select(-notna)
# `summarise()` ungrouping output (override with `.groups` argument)
# # A tibble: 4 x 4
#   line  speaker utterance                                            timestamp                  
#   <chr> <chr>   <chr>                                                <chr>                      
# 1 0001  ID16.1  "ah-ha yes okay so where do we know each other from" 00:00:07.060 - 00:00:22.170
# 2 0004  ID16.2  "U uh Upper Rhine Cruises? maybe? "                  00:00:23.400 - 00:00:26.600
# 3 0005  ID16.3  "yeah? ((pause)) well I do n't-"                     00:00:26.305 - 00:00:28.210
# 4 0006  ID16.1  "(...) Meg? do you know Meg?"                        00:00:27.385 - 00:00:33.879

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...