Pretty straightforward question. Here is the code:
library(dplyr)
library(tidyr)
mtcars %>% group_by(gear) %>% select(hp, disp) %>%
summarise_all(funs(n=sum(!is.na(.)), mean=mean(.,na.rm=T))) %>%
gather(variable, value, -gear) %>%
arrange(gear, sub('_.*', '', variable), sub('.*_', '', variable)) %>%
separate(variable, into = c('var', 'metric'), '_')
I understand everything up to the gather
statement. I'm not familiar with these functions and the help files are not very useful.
Can anyone walk me through this? I'd like to build a function around these commands, but I need to understand how this all works before doing that.
gather
moves from "wide" format to long format, -gear
means don't gather gear
. gather
puts the remaining columns into a single variable
and value
column.
arrange
just sorts by gear, the sub
statements are useless, you could change the arrange
row to arrange(gear, variable)
.
separate
splits the variable column into two using _
as the delimiter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.