Search code examples
rframemeanmedian

Names of males with height under male's median


I am trying to print the Names of males that have height under the median of male's height

I found the median of male's height with the following command:

 y1<-median(ex1[which(ex1$gender=='male')

and i am trying to print the names doing this:

if (gender=='male') 
{ 
  if (height<y1)
  print(Names)

}

Can someone help me? Thanks :)

text file:

        Names height Shoesize gender   Location
  1   andreas    181       44   male citycenter
  4     maria    170       43 female citycenter
  5  xristina    172       43 female citycenter
  13    nikos    175       42   male  outofcity 
  14   kostas    181       44   male  outofcity
  15  giannis    180       43   male  outofcity
  16    eleni    177       43 female  outofcity
  17    panos    133       41   male  outofcity

Solution

  • Just subsetting to create a new data frame

    ex1.medheight <- median(ex1$height[ex1$gender=="male"])
    ex1.shortmales <- ex1[(ex1$height < ex1.medheight && ex1$gender == "male"),]
    

    In the specific case a data frame filtered for males or adding a new column with median disciminator might be better for further working with the data.