Bare with me... I am using the R/RStudio with the data mtcars, dplyr , mutate and the summarise commands. Also tried group by.
I want to center the values mtcars$mpg then take that info and display the summary of the number of cylinders vs centered mtcars$mpg.
So far...
mtcars %>% mutate(centered_mpg = mpg - mean(mpg, na.rm = TRUE)) %>% summarise(centered_mpg, cyl)
The above produces:
| centered_mpg | cyl |
|---|---|
| 0.909375 | 6 |
| 0.909375 | 6 |
| 2.709375 | 4 |
| 1.309375 | 6 |
| ... | ... |
INSTEAD, I WANT:
| centered_mpg | cyl |
|---|---|
| x1 | 4 |
| x2 | 6 |
| x3 | 8 |
mpgfor each group of cars (for eachcylvalue)? Do you want the mean centered mpg by cyl? Or median, sum, something else? I imagine something likemtcars %>% mutate(centered_mpg = mpg - mean(mpg, na.rm = TRUE)) %>% group_by(cyl) %>% summarise(mean_centered_mpg = mean(centered_mpg)), but swap out themean()insidesummarize()for whatever function you'd like. - Gregor Thomas