1

I have the below dataset and trying to calculate the range for Looks of Boys and Girls.

I was trying with the function range but how do i pass Boy or Girl to the Gender? something like Gender='Boy' or Gender = 'Girl'?

> range(~Looks, ~Gender, data = PopularKids)
 Boy1  Boy2 Girl1 Girl2 
 1     4     1     4  

   Age Gender GradeLevel School Location    Goal Grades Sports Looks Money
1  11    Boy          5    Elm    Rural  Sports      1      2     4     3
2  10    Boy          5    Elm    Rural Popular      2      1     4     3
3  11   Girl          5    Elm    Rural Popular      4      3     1     2
4  11   Girl          5    Elm    Rural Popular      2      3     4     1
5  10   Girl          5    Elm    Rural Popular      4      2     1     3
6  11   Girl          5    Elm    Rural Popular      4      2     1     3
bigskull
  • 739
  • 2
  • 13
  • 23

2 Answers2

1

We can use

library(dplyr)
PopularKids %>%
    group_by(Gender) %>%
    summarise(Min = min(Looks), Max = max(Looks))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

With data.table:

library(data.table)

df <- structure(list(Age = c(11L, 10L, 11L, 11L, 10L, 11L), 
                     Gender = c("Boy","Boy", "Girl", "Girl", "Girl", "Girl"), 
                     GradeLevel = c(5L, 5L, 5L, 5L, 5L, 5L),
                     School = c("Elm", "Elm", "Elm", "Elm", "Elm", "Elm"), 
                     Location = c("Rural", "Rural", "Rural", "Rural","Rural","Rural"),
                     Goal = c("Sports", "Popular", "Popular", "Popular",  "Popular", "Popular"), Grades = c(1L, 2L, 4L, 2L, 4L, 4L), 
                     Sports = c(2L,1L, 3L, 3L, 2L, 2L), Looks = c(4L, 4L, 1L, 4L, 1L, 1L), Money = c(3L,3L, 2L, 1L, 3L, 3L)), class = c("data.frame"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                     -6L))

setDT(df)
df[,.(range=list(range(Looks))),by=.(Gender)]
#>    Gender range
#> 1:    Boy   4,4
#> 2:   Girl   1,4

Waldi
  • 39,242
  • 6
  • 30
  • 78