count - Using Group by in R using dplyr -


this question has answer here:

apologies , understand question little vague. working on live environment , please find blinded test data below :-

structure(list(rco = structure(c(1l, 1l, 1l, 2l, 2l, 2l), .label = c("a220",  "b334"), class = "factor"), month = structure(c(2l, 1l, 3l, 2l,  1l, 3l), .label = c("feb", "jan", "mar"), class = "factor"),      count = c(12, 22, 33, 44, 55, 66)), .names = c("rco", "month",  "count"), row.names = c(na, 6l), class = "data.frame") 

i proceeding generate bar chart on data(using rcharts) follows :-

b <- hplot(x = 'rco', y = 'count', data = test, type = 'bar', group = 'month',title = "aaaaaa", subtitle = "bar graph") 

now serves purpose values when have cases data not exist months (please case below) incorrect output :-

test <- structure(list(rco = structure(c(1l, 1l, 2l, 2l, 2l, 3l), .label = c("a220","b334", "c123"), class = "factor"), month = structure(c(2l, 1l, 3l, 2l, 1l, 2l), .label = c("feb", "jan", "mar"), class = "factor"), count = c(12, 22, 33, 44, 55, 66)), .names = c("rco", "month", "count"), row.names = c(na, 6l), class = "data.frame")

b <- hplot(x = 'rco', y = 'count', data = test, type = 'bar', group = 'month',title = "aaaaaa", subtitle = "bar graph") 

please run rcharts::hplot function on data , find bars representing codes mixed (for c123 , b334).

i not find reference on internet states issue in rcharts, thought handle making rows each rco , padding them zero's can have data frame 1 record per r co per month . also, there might more 4 months - data year or 2 , must make dynamic.

any appreciated.

thanks in advance

beetroot's answer should fine, here 'tidyverse' solution:

df %>%    count(first.name, sex) %>%    ungroup() %>%    tidyr::complete(first.name, sex, fill = list(n = 0)) 

results in:

source: local data frame [8 x 3]    first.name    sex     n       (fctr) (fctr) (dbl) 1      edgar female     0 2      edgar   male     1 3       jane female     1 4       jane   male     0 5       john female     0 6       john   male     1 7       walt female     0 8       walt   male     1 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -