R mutate() working with paste0()

10,232

Solution 1

Remove the collapse (and add a " " inside the paste0 function) to match the result when you used paste:

mtcars %>% group_by(cyl) %>% ungroup() %>% mutate(`newcol ss` = paste0(as.character(wt), " ", as.character(drat)))

You can produce your weird result using paste as well:

mtcars %>% group_by(cyl) %>% ungroup() %>% mutate(`newcol ss` = paste(as.character(wt), as.character(drat), collapse = ";"))

> mtcars %>% group_by(cyl) %>% ungroup() %>% mutate(`newcol ss` = paste(as.character(wt), as.character(drat), collapse = ";"))
# A tibble: 32 x 13
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb ss           `newcol ss`                                                         
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>        <chr>                                                               
 1  21.0    6.  160.  110.  3.90  2.62  16.5    0.    1.    4.    4. "2.623.9 "   2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
 2  21.0    6.  160.  110.  3.90  2.88  17.0    0.    1.    4.    4. "2.8753.9 "  2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
 3  22.8    4.  108.   93.  3.85  2.32  18.6    1.    1.    4.    1. "2.323.85 "  2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
 4  21.4    6.  258.  110.  3.08  3.22  19.4    1.    0.    3.    1. "3.2153.08 " 2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
 5  18.7    8.  360.  175.  3.15  3.44  17.0    0.    0.    3.    2. "3.443.15 "  2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
 6  18.1    6.  225.  105.  2.76  3.46  20.2    1.    0.    3.    1. "3.462.76 "  2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
 7  14.3    8.  360.  245.  3.21  3.57  15.8    0.    0.    3.    4. "3.573.21 "  2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
 8  24.4    4.  147.   62.  3.69  3.19  20.0    1.    0.    4.    2. "3.193.69 "  2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
 9  22.8    4.  141.   95.  3.92  3.15  22.9    1.    0.    4.    2. "3.153.92 "  2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
10  19.2    6.  168.  123.  3.92  3.44  18.3    1.    0.    4.    4. "3.443.92 "  2.62 3.9;2.875 3.9;2.32 3.85;3.215 3.08;3.44 3.15;3.46 2.76;3.57 3.~
# ... with 22 more rows

Personally I'd use less complex code:

mtcars$ss <- paste0(mtcars$wt, " ", mtcars$drat)

Solution 2

collapse = ", " is the cause of the problem.

Try putting collapse in paste, same result will be obtained, except, space will be added between values of two columns.

It is not the differences in paste and paste0 which gave difference in results.

Try understanding usage of paste and paste0 and usage of collapse.

[[Also as griffinevo and Kim have mentioned in the comments above, stick with simpler code. ]]

Share:
10,232
Liang
Author by

Liang

Updated on October 19, 2022

Comments

  • Liang
    Liang over 1 year

    I just observed a very weird behaviour that I cannot explain when comparing paste0 and paste applied with mutate:

    mtcars %>% group_by(cyl) %>% ungroup() %>% mutate(`newcol ss` = paste0(as.character(wt), as.character(drat), collapse=";"))
    

    The output of the above piece of code using paste0 is as follows:

    enter image description here

    mtcars %>% group_by(cyl) %>% ungroup() %>% mutate(`newcol ss` = paste(as.character(wt), as.character(drat)))
    

    The output of the paste() function is as follows: enter image description here

    Basically mutate works well with paste (combine two column by row) while the paste0 combines all columns and rows in a single cell.

    • rg255
      rg255 about 6 years
      Why are you bothering with such a complex piece of code? Couldn't you just use mtcars$ss <- paste0(mtcars$wt, mtcars$drat, sep = " ")
    • rg255
      rg255 about 6 years
      Also paste0 has a collapse specified, and paste doesn't... That's why rdocumentation.org/packages/base/versions/3.4.3/topics/paste
    • Kim
      Kim about 6 years
      If you do mtcars %>% group_by(cyl) %>% ungroup() %>% mutate(`newcol ss` = paste(as.character(wt), as.character(drat), collapse = ";")), it combines the entire wt and drat columns as well. So it's not a difference between paste and paste0.
    • Liang
      Liang almost 6 years
      @griffinevo sorry for the complex code, just try to reproduce the same problem I came across in a project. But here my question is why all columns are combined in the first scenario.
    • rg255
      rg255 almost 6 years
      The collapse argument - as noted in my answer - is nor equal in your two scenarios. That is causing the difference.