Harry M
Harry M

Reputation: 2088

Using the unite function in R and removing duplicated values

I'm trying to use the unite function in R to concatenate values across columns, but also deduplicate the values. How can I accomplish this?

Here is the input data:

input <- tibble(
  id = c('aa', 'ss', 'dd', 'qq'),
  '2017' = c('tv', NA, NA, 'web'),
  '2018' = c('tv', 'web', NA, NA),
  '2019' = c(NA, 'web', 'book', 'tv')
)

# A tibble: 4 x 4
  id    `2017` `2018` `2019`
  <chr> <chr>  <chr>  <chr> 
1 aa    tv     tv     NA    
2 ss    NA     web    web    
3 dd    NA     NA     book  
4 qq    web    NA     tv    

The desired output with the ALL column is:

> output
# A tibble: 4 x 5
  id    `2017` `2018` `2019` ALL   
  <chr> <chr>  <chr>  <chr>  <chr> 
1 aa    tv     tv     NA     tv    
2 ss    NA     web    web    web   
3 dd    NA     NA     book   book  
4 qq    web    NA     tv     web, tv

Upvotes: 2

Views: 1667

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389175

I am not sure if deduplicating is possible with unite, however you can use apply row-wise.

input$ALL <- apply(input[-1], 1, function(x) toString(na.omit(unique(x))))

Or a tidyverse way could be using pmap

library(tidyverse)

input %>%
  mutate(ALL = pmap_chr(select(., -id), ~toString(unique(na.omit(c(...))))))

#  id    `2017` `2018` `2019` ALL    
#  <chr> <chr>  <chr>  <chr>  <chr>  
#1 aa    tv     tv     NA     tv     
#2 ss    NA     web    web    web    
#3 dd    NA     NA     book   book   
#4 qq    web    NA     tv     web, tv

Or getting the data in long format and then joining

input %>%
  pivot_longer(cols = -id, values_drop_na = TRUE) %>%
  group_by(id) %>%
  summarise(ALL = toString(unique(value))) %>%
  left_join(input)

Upvotes: 4

Maurits Evers
Maurits Evers

Reputation: 50718

Similar questions exist here on SO, but since you are after a unite solution and I couldn't find any that specifically use unite, here we go:

Using unite

input %>% unite(ALL, -id, sep = ", ", remove = FALSE, na.rm = TRUE)
## A tibble: 4 x 5
#  id    ALL     `2017` `2018` `2019`
#  <chr> <chr>   <chr>  <chr>  <chr>
#1 aa    tv      tv     NA     NA
#2 ss    web     NA     web    NA
#3 dd    book    NA     NA     book
#4 qq    web, tv web    NA     tv

To recover the exact column order of your expected output, you can add a %>% select(names(input), ALL).

Alternatively, using nest

input %>%
    group_by(id) %>%
    nest() %>%
    mutate(ALL = map_chr(data, ~toString(unlist(.x[!is.na(unlist(.x))])))) %>%
    unnest(data)
## A tibble: 4 x 5
## Groups:   id [4]
#  id    `2017` `2018` `2019` ALL
#  <chr> <chr>  <chr>  <chr>  <chr>
#1 aa    tv     NA     NA     tv
#2 ss    NA     web    NA     web
#3 dd    NA     NA     book   book
#4 qq    web    NA     tv     web, tv 

Or the base R way (as in How to create new column with all non-NA values from multiple other columns?):

input$ALL <- apply(input[, -1], 1, function(x) toString(x[!is.na(x)]))
input
# A tibble: 4 x 5
#   id    `2017` `2018` `2019` ALL
#  <chr> <chr>  <chr>  <chr>  <chr>
#1 aa    tv     NA     NA     tv
#2 ss    NA     web    NA     web
#3 dd    NA     NA     book   book
#4 qq    web    NA     tv     web, tv

Upvotes: 1

Related Questions