0

Is there any way to concatenate two variables together using only dplyr commands?

For example:

df <- mtcars
df <- select(df, mpg, cyl)
df$mpg <- as.character(df$mpg)
df$cyl <- as.character(df$cyl)

df <- unite(df, "new_var", c(mpg, cyl), sep="", remove = FALSE)

view(df)

I realize that the unite function makes this very simple, but I can't use it as I'm trying to do this operation on an object of type tbl_MariaDBConnection, which, as far as I understand, can only be operated on with dplyr commands. Thanks!

dd_data
  • 93
  • 5

1 Answers1

2

Using mutate+paste0 will do the trick

df <- mtcars
df <- select(df, mpg, cyl)
df$mpg <- as.character(df$mpg)
df$cyl <- as.character(df$cyl)
df <- df %>% 
 mutate(new_var = paste0(mpg, cyl))
View(df)

For all dplyr : transmute = mutate, but deletes everything else.

df <- mtcars %>% 
  dplyr::transmute(
    mpg = as.character(mpg),
    cyl = as.character(cyl),
    new_var = paste0(mpg, cyl)
 )
df
olivroy
  • 548
  • 3
  • 13