1

Apologies and now I understand my question was a little vague. I am working on a live environment and please find some 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 am proceeding then to generate a bar chart on this data(using Rcharts) as follows :-

b <- hPlot(x = 'Rco', y = 'Count', data = test, type = 'bar', group = 'month',title = "AAAAAA", subtitle = "Bar Graph")

Now this serves my purpose perfectly values but when I have cases where the data does not exist for all Months (please that case below) I get 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 the Rcharts::hplot function on the data and you will find that the bars representing the codes are mixed up (for C123 and B334).

I could not find any reference on the internet which states that this is an issue in Rcharts, so I thought I would handle it by making extra rows for each Rco and padding them with zero's so that I can have a data frame with one record per R co per month . Also, there might be more than 4 months - data could be for a year or 2 , so I must make it dynamic.

Any help would be greatly appreciated.

Thanks in advance

rantish
  • 79
  • 1
  • 8

2 Answers2

3

You can use table() (assuming your data frame is called data):

table(data$First.Name, data$Sex)

        FEMALE MALE
  Edgar      0    1
  Jane       1    0
  John       0    1
  Walt       0    1
erc
  • 10,113
  • 11
  • 57
  • 88
2

beetroot's answer should do just fine, but here is a '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
Axeman
  • 32,068
  • 8
  • 81
  • 94