Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate string columns and order in alphabetical order

Tags:

r

library(purrr)
library(stringr)
library(tidyr)
library(dplyr)
Text = c("A little bird told me about the dog", "A pig in a poke", "As busy as a bee")
data = as.data.frame(Text)
keywords <- paste0(c("bird", "dog", "pig","wolf","cat", "bee", "turtle"), collapse = "|")
data %>% 
  mutate(Words = str_extract_all(Text, keywords),
        Words = map(Words, ~ as.list(unique(.x)) %>% 
                              set_names(str_c('col', seq_along(.))))) %>%
  unnest_wider(Words)

This question is a continuation of this other question I asked (Question)[Extract words from text using dplyr and stringr.

Now I'm trying to find a way to concatenate the extracted columns in alphabetical order, but the only way that I find was making comparisons of type

col1 < col2 then col1 + col2
col1 > col2 then col2 + col1

Unfortunately this needs a lot of work for more than 3 columns. The output I desire is like this

col1 col2 col3 concatenated_string
dog  cat  bird bird + cat + dog
fish bird dog  bird + dog + fish
like image 803
Roland Avatar asked Dec 15 '25 02:12

Roland


2 Answers

Using pmap_chr and sort

library(purrr)
library(dplyr)
df %>% mutate(cs=pmap_chr(list(col1,col2,col3), ~paste(sort(c(...)), collapse = " + ")))

  col1 col2 col3                cs
1  dog  cat bird  bird + cat + dog
2 fish bird  dog bird + dog + fish
like image 101
A. Suliman Avatar answered Dec 16 '25 19:12

A. Suliman


Using apply you could do this...

data$concat <- apply(data[,-1], 1, function(x) paste(sort(x), collapse = "+"))

data
  Text                                col1  col2  concat    
1 A little bird told me about the dog bird  dog   bird+dog
2 A pig in a poke                     pig   NA    pig       
3 As busy as a bee                    bee   NA    bee   
like image 30
Andrew Gustar Avatar answered Dec 16 '25 19:12

Andrew Gustar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!