I’m struggling with a seemingly simple task: in df
I want to summarise data in words
into strings (no problem) and also the respective Duration
s of these words
(problem). My attempt at subsetting Duration
on the condition that [!is.na(words)]
fails; instead all values in Duration
get stringed together:
library(tidyverse)
df %>%
summarise(
words = str_c(words[!is.na(words)], collapse = ","),
words_dur = str_c(Duration[!is.na(words)], collapse = ",")
)
words words_dur
1 hey,how,are,you 44,150,30,55,77,80,99,100,200
The expected output is this:
words words_dur
1 hey,how,are,you 44,55,77,99
Data:
df <- data.frame(
words = c("hey", NA, NA, "how", "are", NA, "you", NA, NA),
Duration = c(44, 150, 30, 55, 77, 80, 99, 100, 200)
)