Convenience function to paste together multiple columns into one.

unite(.df, col = ".united", ..., sep = "_", remove = TRUE, na.rm = FALSE)

Arguments

.df

A data.frame or data.table

col

Name of the new column, as a string.

...

Selection of columns. If empty all variables are selected. tidyselect compatible.

sep

Separator to use between values

remove

If TRUE, removes input columns from the data.table.

na.rm

If TRUE, NA values will be not be part of the concatenation

Examples

df <- tidytable(
    a = c("a", "a", "a"),
    b = c("b", "b", "b"),
    c = c("c", "c", NA)
)

df %>%
  unite("new_col", b, c)
#> # A tidytable: 3 × 2
#>   a     new_col
#>   <chr> <chr>  
#> 1 a     b_c    
#> 2 a     b_c    
#> 3 a     b_NA   

df %>%
  unite("new_col", where(is.character))
#> # A tidytable: 3 × 1
#>   new_col
#>   <chr>  
#> 1 a_b_c  
#> 2 a_b_c  
#> 3 a_b_NA 

df %>%
  unite("new_col", b, c, remove = FALSE)
#> # A tidytable: 3 × 4
#>   a     new_col b     c    
#>   <chr> <chr>   <chr> <chr>
#> 1 a     b_c     b     c    
#> 2 a     b_c     b     c    
#> 3 a     b_NA    b     NA   

df %>%
  unite("new_col", b, c, na.rm = TRUE)
#> # A tidytable: 3 × 2
#>   a     new_col
#>   <chr> <chr>  
#> 1 a     b_c    
#> 2 a     b_c    
#> 3 a     b      

df %>%
  unite()
#> # A tidytable: 3 × 1
#>   .united
#>   <chr>  
#> 1 a_b_c  
#> 2 a_b_c  
#> 3 a_b_NA