Instantly share code, notes, and snippets.

Embed
What would you like to do?
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