Reputation: 1613
I have a dataframe like the following:
df = data.frame(ID = c("Text 1.1", "Text 1.2", "Text 1.3", "Text 1.4", "Text 2.1", "Text 2.2", "Text 2.3"), Text = c("Hello", "Hello World", "World", "Ciao", "Ciao Ciao", "SO will fix it", "World is great"))
ID Text
1 Text 1.1 Hello
2 Text 1.2 Hello World
3 Text 1.3 World
4 Text 1.4 Ciao
5 Text 2.1 Ciao Ciao
6 Text 2.2 SO will fix it
7 Text 2.3 World is great
I would like to achieve this:
ID Text
1 Text 1 Hello Hello World World Ciao
2 Text 2 Ciao Ciao SO will fix it World is great
Basically, I would like to paste the text in the second column for every 1.1,...,1.n
; 2.1,...,2.n
, etc.
Can anyone help me?
Thanks!
Upvotes: 0
Views: 338
Reputation: 11548
Does this work:
> library(dplyr)
> library(tidyr)
> df %>% separate(col = ID, into = c('ID','Sub-ID'), sep = '\\.') %>% group_by(ID) %>% summarise(Text = paste0(Text, collapse = ' '))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 2 x 2
ID Text
<chr> <chr>
1 Text 1 Hello Hello World World Ciao
2 Text 2 Ciao Ciao SO will fix it World is great
>
Upvotes: 2
Reputation: 146249
library(dplyr)
library(lubridate)
df %>%
# extract everything before the "." in ID
mutate(group = str_extract(ID, ".*(?=\\.)")) %>%
group_by(group) %>%
# paste together
summarize(Text = paste(Text, collapse = " "))
# # A tibble: 2 x 2
# group Text
# <chr> <chr>
# 1 Text 1 Hello Hello World World Ciao
# 2 Text 2 Ciao Ciao SO will fix it World is great
Upvotes: 2