I am trying to reproduce and understand a colleague's code in my system
library(tidyverse)
data <- mtcars
tabl1 <- data %>%
mutate(cyl = as.character(cyl)) %>%
bind_rows(mutate(., cyl = "all")) %>%
group_by(cyl) %>%
summarise(mpg_q25 = quantile(mpg, prob = .25),
mpg_q50 = quantile(mpg, prob = .50),
mpg_q75 = quantile(mpg, prob = .75),
count = n())
tabl1
It is supposed to give me the following result:
# A tibble: 4 × 5
cyl mpg_q25 mpg_q50 mpg_q75 count
<chr> <dbl> <dbl> <dbl> <int>
1 4 22.8 26 30.4 11
2 6 18.6 19.7 21 7
3 8 14.4 15.2 16.2 14
4 all 15.4 19.2 22.8 32
However I am ending up with the following error:
Error in `bind_rows()`:
! Can't combine `..1$cyl` <double> and `..2$cyl` <character>.
Can someone explain what am I missing here? Also it would be great help if you can help me understand the syntax of mutate here.
EDIT: tried the answer posted by @akrun which resolved the character issue. However a new error has popped up as below:
no applicable method for 'mutate' applied to an object of class "character"
How do I solve this? Also explain the "." used as first argument of mutate in bind rows?