Convenience function to paste together multiple columns.
Arguments
- data
A
data.frame
.- col
character(1)
orsymbol(1)
. The name of the new column.- ...
The columns to unite.
- sep
character(1)
. Separator to use between the values.- remove
logical(1)
. IfTRUE
, remove the input columns from the outputdata.frame
.- na.rm
logical(1)
. IfTRUE
, missing values will be remove prior to uniting each value.
Examples
df <- data.frame(x = c("a", "a", NA, NA), y = c("b", NA, "b", NA))
df
#> x y
#> 1 a b
#> 2 a <NA>
#> 3 <NA> b
#> 4 <NA> <NA>
df %>% unite("z", x:y, remove = FALSE)
#> z x y
#> 1 a_b a b
#> 2 a_NA a <NA>
#> 3 NA_b <NA> b
#> 4 NA_NA <NA> <NA>
# To remove missing values:
df %>% unite("z", x:y, na.rm = TRUE, remove = FALSE)
#> z x y
#> 1 a_b a b
#> 2 a a <NA>
#> 3 b <NA> b
#> 4 <NA> <NA>