r - Group medians from a data frame using dplyr -


computing medians seems a bit of achilles heel r (ie. no data.frame method). least amount of typing needed group medians data frame using dplyr?

my_data <- structure(list(group = c("group 1", "group 1", "group 1", "group 1",  "group 1", "group 1", "group 1", "group 1", "group 1", "group 1",  "group 1", "group 1", "group 1", "group 1", "group 1", "group 2",  "group 2", "group 2", "group 2", "group 2", "group 2", "group 2",  "group 2", "group 2", "group 2", "group 2", "group 2", "group 2",  "group 2", "group 2"), value = c("5", "3", "6", "8", "10", "13",  "1", "4", "18", "4", "7", "9", "14", "15", "17", "7", "3", "9",  "10", "33", "15", "18", "6", "20", "30", na, na, na, na, na)), .names = c("group",  "value"), class = c("tbl_df", "data.frame"), row.names = c(na,  -30l))  library(dplyr)    # groups 1 & 2 my_data_groups_1_and_2 <- my_data[my_data$group %in% c("group 1", "group 2"), ]  # compute medians per group medians <- my_data_groups_1_and_2 %>%   group_by(group) %>%   summarize(the_medians = median(value, na.rm = true))  

which gives:

error in summarise_impl(.data, dots) :    string_elt() can applied 'character vector', not 'double' in addition: warning message: in mean.default(sort(x, partial = half + 0l:1l)[half + 0l:1l]) :   argument not numeric or logical: returning na 

what least effort workaround here?

as commented ivyleavedtoadflax, error caused supplying non-numeric or non-logical argument median, since value column of type character (you can tell not numeric seeing numbers quoted). here 2 simple ways solve it:

my_data %>%    filter(group %in% c("group 1", "group 2")) %>%   group_by(group) %>%   summarize(the_medians = median(as.numeric(value), na.rm = true))  

or

my_data %>%    filter(group %in% c("group 1", "group 2")) %>%   mutate(value = as.numeric(value))  %>%   group_by(group) %>%   summarize(the_medians = median(value, na.rm = true))  

to check structure including type of columns in data, conveniently use

str(my_data) #classes ‘tbl_df’ , 'data.frame': 30 obs. of  2 variables: # $ group: chr  "group 1" "group 1" "group 1" "group 1" ... # $ value: chr  "5" "3" "6" "8" ... 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -