0

I am trying to figure out how to extract all the unique characters from a certain column. For example, if one of my column has the following rows,

june  
july&  
august%

then I would like r to give me the list of all the unique characters, i.e,

junely&agst%

How can this be done in R?

Salahuddin
  • 37
  • 1
  • 11

2 Answers2

3

Split the column values at each character and paste only unique characters.

x <- c('june', 'july&', 'august%')
paste0(unique(unlist(strsplit(x, ''))), collapse = "")
#[1] "junely&agst%"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

May be a Tidy approach will be useful:

library(dplyr)
library(purrr)
library(stringr)

# input
x <- c("june", "july&", "august%")
expected <- "junely&agst%"


# modify
actual <- x %>% str_split(pattern = "") %>% flatten_chr %>% unique %>% paste0(collapse = "")


# validate
stopifnot(actual == expected)
codez0mb1e
  • 706
  • 6
  • 17