By adamjhodgson


2018-04-17 21:58:53 8 Comments

I have a dataset simplified as follows:

print(data)
Team   Colour
A      Red
B      Red
C      Blue
D      Green

I want to produce a waffle chart (square pie chart) of this data. I can do this very easily through:

waffle(c(Red = sum(data$Colour == "Red"), 
         Blue = sum(data$Colour = "Blue"), 
         Green = sum(data$colour = "Green")))

However, I cannot seem to find an easy way to produce the named integer created within the waffle function above, through manipulation of my dataset. I need it to be produced directly from the original dataset as I will need it to be a reactive function within a shiny app.

I have tried using group_by within dplyr:

forwaffle <- data%>%
             group_by(Colour)
             summarise(frequency=n())

However, I cannot convert this list into the required named integer. I have tried using t() and unlist() but neither of these achieve my desired result.

2 comments

@Maurits Evers 2018-04-17 22:18:14

Update

A shorter tidyverse solution using deframe (thanks to @r2evans):

count(df, Colour) %>%
    tibble::deframe() %>%
    waffle(.)

The solution by @r2evans in the comment above is definitely the most concise but if you insist on using a tidyverse approach you can do:

setNames(
    df %>% count(Colour) %>% pull(n),
    df %>% count(Colour) %>% pull(Colour)) %>%
waffle(.)

enter image description here


Sample data

df <- read.table(text =
    "Team   Colour
A      Red
B      Red
C      Blue
D      Green", header = T)

@r2evans 2018-04-17 22:29:06

Alternatives: count(df, Colour) %>% tibble::deframe() or ... %>% with(., setNames(n, Colour)).

@Maurits Evers 2018-04-17 22:54:09

Aah, deframe was what I was looking for!! Thanks @r2evans. I hope you don't mind if I add this to the solution.

@r2evans 2018-04-17 22:59:10

Feel free! I much prefer calculating things only once, the double-pipe method was making me twitch ;-)

@Maurits Evers 2018-04-17 23:10:08

@r2evans Yes the double-pipe is awful;-) was trying to avoid but went down the wrong path trying (and failing) to use purrr:map...

@r2evans 2018-04-18 01:22:29

Right, map would have done alright too. It might have been best to save the count results and work off them, but then again SO can be a "hasty solutions" site, not always worth the effort to put in more than the gist.

@neilfws 2018-04-17 22:17:08

If by "required named integer" you mean that the colours should be column names, then you can use tidyr::spread:

library(dplyr)
library(tidyr)

data %>% 
  group_by(Colour) %>% 
  summarise(frequency = n()) %>% 
  spread(Colour, frequency)

# A tibble: 1 x 3
   Blue Green   Red
  <int> <int> <int>
1     1     1     2

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] Drop data frame columns by name

  • 2011-01-05 14:34:29
  • Btibert3
  • 1214844 View
  • 761 Score
  • 19 Answer
  • Tags:   r dataframe r-faq

18 Answered Questions

[SOLVED] How to find out if an item is present in a std::vector?

  • 2009-02-20 21:58:41
  • Joan Venge
  • 831424 View
  • 562 Score
  • 18 Answer
  • Tags:   c++ vector std

18 Answered Questions

[SOLVED] Concatenating two std::vectors

1 Answered Questions

7 Answered Questions

[SOLVED] Test if a vector contains a given element

  • 2009-07-23 02:20:53
  • medriscoll
  • 559395 View
  • 449 Score
  • 7 Answer
  • Tags:   r vector r-faq

1 Answered Questions

[SOLVED] Create new dataframe after uploading csv in shiny

  • 2017-12-07 22:18:51
  • user124123
  • 308 View
  • 2 Score
  • 1 Answer
  • Tags:   r shiny

4 Answered Questions

[SOLVED] Appending a vector to a vector

  • 2010-03-31 09:33:02
  • sub
  • 453162 View
  • 581 Score
  • 4 Answer
  • Tags:   c++ stl vector

0 Answered Questions

dplyr bizarre ERROR: group_by()%>%summarise() outputs just one line

  • 2015-06-15 19:59:00
  • LucasMation
  • 136 View
  • 0 Score
  • 0 Answer
  • Tags:   r group-by dplyr

1 Answered Questions

Calling the filename from a reactive dataset in shiny r

1 Answered Questions

[SOLVED] dplyr R group totals differ between factors

  • 2015-01-22 18:42:14
  • marty_c
  • 134 View
  • 2 Score
  • 1 Answer
  • Tags:   r dplyr

Sponsored Content