Skip to content

Instantly share code, notes, and snippets.

@EmilHvitfeldt
Created October 26, 2017 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EmilHvitfeldt/6d936c8af4d2dd1556669d367f0a6056 to your computer and use it in GitHub Desktop.
Save EmilHvitfeldt/6d936c8af4d2dd1556669d367f0a6056 to your computer and use it in GitHub Desktop.
Similarly rated candies in the candy hierarchy data for 2017
library(tidyverse)
library(stringr)
require(visNetwork)
candy <- read_csv("candyhierarchy2017.csv")
names(candy) <- names(candy) %>% str_replace_all("\\\\xd5", "")
balanced_candy <- candy %>%
mutate(total_na = candy %>%
select(starts_with("Q6")) %>%
is.na %>% rowSums) %>%
filter(total_na == 0)
sub_candy <- balanced_candy %>%
gather(starts_with("Q6"), key = "candy", value = "feeling")
combination_candy <- combn(sub_candy$candy %>% unique(), 2)
compare_candy <- map_dbl(combination_candy %>% as.tibble,
~ (balanced_candy %>% pull(.x[1]) ==
balanced_candy %>% pull(.x[2])) %>% mean()) %>%
rbind(combination_candy) %>% t() %>% as.tibble()
names(compare_candy) <- c("compare", "to", "from")
nodes <- tibble(id = sub_candy$candy %>% unique()) %>%
mutate(label = str_wrap(id, width = 20))
edges <- compare_candy %>%
mutate(compare = as.numeric(compare),
width = (compare - 0.75) * 50,
color = "grey") %>%
filter(compare > 0.75)
visNetwork(nodes, edges, width = "100%", height = 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment