Katie Truong - 1 year ago 64
R Question

Calculate stats in concatenated strings in R

Suppose I have a dataframe like this:

``````     X.                      Name             Type Total  HP Attack Defense Sp..Atk Sp..Def Speed
795 718          Zygarde50% Forme    Dragon/Ground   600 108    100     121      81      95    95
796 719                   Diancie       Rock/Fairy   600  50    100     150     100     150    50
797 719       DiancieMega Diancie       Rock/Fairy   700  50    160     110     160     110   110
798 720       HoopaHoopa Confined    Psychic/Ghost   600  80    110      60     150     130    70
799 720        HoopaHoopa Unbound     Psychic/Dark   680  80    160      60     170     130    80
800 721                 Volcanion       Fire/Water   600  80    110     120     130      90    70
``````

If I want to calculate the average stats (Total, HP, Attack, Defense, etc...), per type Dragon, type Ground, type Rock, type Fairy, etc... (instead of type Dragon/Ground, Rock/Fairy), how would I proceed? The stats of pokemons that belong to any two types would be used in calculating the average stats for both.

I have written the code using functions in the
`dplyr`
package:

``````summaryStats_byType<- summarise(byType,
count = n(),
averageTotal = mean(Total, na.rm = T),
averageHP = mean(HP, na.rm = T),
averageDefense = mean(Defense, na.rm = T),
averageSpAtk = mean(Sp..Atk, na.rm = T),
averageSpDef = mean(Sp..Def, na.rm = T),
averageSpeed = mean(Speed, na.rm = T))
``````

but obviously it counts "Dragon/Ground" as a type instead of two.

One way is to split the `Type` column in long format (I chose `cSplit` from `splitstackshape` to do this) and `group_by` as usual, i.e.

``````library(splitstackshape)
library(dplyr)

df1 <- cSplit(df, 'Type', sep = '/', 'long')
df1 %>%
group_by(Type) %>%
summarise_each(funs(mean), -c(X., Name))

# A tibble: 9 × 8
#     Type Total    HP Attack Defense Sp..Atk Sp..Def Speed
#   <fctr> <dbl> <dbl>  <dbl>   <dbl>   <dbl>   <dbl> <dbl>
#1    Dark   680    80    160      60     170     130    80
#2  Dragon   600   108    100     121      81      95    95
#3   Fairy   650    50    130     130     130     130    80
#4    Fire   600    80    110     120     130      90    70
#5   Ghost   600    80    110      60     150     130    70
#6  Ground   600   108    100     121      81      95    95
#7 Psychic   640    80    135      60     160     130    75
#8    Rock   650    50    130     130     130     130    80
#9   Water   600    80    110     120     130      90    70
``````

Alternatively (as noted by @DavidArenburg) we can also use `separate_rows` from `tidyr` as part of the pipe, i.e.

``````library(tidyr)
library(dplyr)
df %>%
separate_rows(Type) %>%
group_by(Type) %>%
summarise_each(funs(mean), -c(X., Name))
``````

which of course yields the same results

DATA

``````dput(df)
structure(list(X. = c(718L, 719L, 719L, 720L, 720L, 721L), Name = structure(c(6L,
1L, 2L, 3L, 4L, 5L), .Label = c("Diancie", "DiancieMega_Diancie",
"HoopaHoopa_Confined", "HoopaHoopa_Unbound", "Volcanion", "Zygarde50%_Forme"
), class = "factor"), Type = structure(c(1L, 5L, 5L, 4L, 3L,
2L), .Label = c("Dragon/Ground", "Fire/Water", "Psychic/Dark",
"Psychic/Ghost", "Rock/Fairy"), class = "factor"), Total = c(600L,
600L, 700L, 600L, 680L, 600L), HP = c(108L, 50L, 50L, 80L, 80L,
80L), Attack = c(100L, 100L, 160L, 110L, 160L, 110L), Defense = c(121L,
150L, 110L, 60L, 60L, 120L), Sp..Atk = c(81L, 100L, 160L, 150L,
170L, 130L), Sp..Def = c(95L, 150L, 110L, 130L, 130L, 90L), Speed = c(95L,
50L, 110L, 70L, 80L, 70L)), .Names = c("X.", "Name", "Type",
"Total", "HP", "Attack", "Defense", "Sp..Atk", "Sp..Def", "Speed"
), class = "data.frame", row.names = c("795", "796", "797", "798",
"799", "800"))
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download