I'm working with the League of Legends Champions dataset
name | tags |
---|---|
Aatrox | Fighter |
Ahri | Mage,Assassin |
Akali | Assassin |
Akshan | Marksman,Assassin |
Alistar | Tank,Support |
And I was wondering how to count the total champions per tag, I'm really new in R, so I made a solution that I think is very bad and I know that may be a function that can help with this.
Here are the packages that I'm using
library(tidyverse)
library(janitor)
First, I separate my tags in different columns
lol_champions_tags <- lol_champions_clean %>%
select(name, tags) %>%
separate(tags, sep = ',', into = paste0('tag', 1:2))
Then, I calculate the total entries per tag
lol_champions_tags %>%
select(name, tag1) %>%
group_by(tag1) %>%
summarise(N = n()) -> tag1
lol_champions_tags %>%
select(name, tag2) %>%
drop_na() %>%
group_by(tag2) %>%
summarise(N = n()) -> tag2
And finally I put all together and made the addition
champions_per_tag <-
bind_cols(tag1, tag2) %>%
clean_names() %>%
select(tag1,n_2,n_4) %>%
mutate(total_entries = n_2 + n_4) %>%
select(tag1, total_entries)
I was wondering if there was better way to do this or if there is a function that could handle this type of situation.
Maybe you want just this one-liner
> as.data.frame(table(unlist(strsplit(lol_champs$tags, ",", fixed=TRUE))))
Var1 Freq
1 Assassin 3
2 Fighter 1
3 Mage 1
4 Marksman 1
5 Support 1
6 Tank 1
The last code section appears unclear to, so I do not replicate it.
lol_champs = read.table(text="name tags
Aatrox Fighter
Ahri Mage,Assassin
Akali Assassin
Akshan Marksman,Assassin
Alistar Tank,Support", header=TRUE)