Home > Back-end >  selecting variable from a column
selecting variable from a column

Time:05-08

enter image description here

starwars <- merge(starwars, num, sort =TRUE)
starwars
sex_male <-subset(starwars ,select=c("male")
 sex_male

using rstudio dplyr how do I narrowdown the dataframe to just male sex?

CodePudding user response:

You need filter and ==

library(dplyr)

sex_male <- starwars %>% 
  filter(sex == "male")

CodePudding user response:

Another option is using this:

starwars[starwars$sex == 'male',]

Output:

# A tibble: 64 × 14
   name         height  mass hair_color skin_color eye_color birth_year sex   gender homeworld
   <chr>         <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr>  <chr>    
 1 Luke Skywal…    172    77 blond      fair       blue            19   male  mascu… Tatooine 
 2 Darth Vader     202   136 none       white      yellow          41.9 male  mascu… Tatooine 
 3 Owen Lars       178   120 brown, gr… light      blue            52   male  mascu… Tatooine 
 4 Biggs Darkl…    183    84 black      light      brown           24   male  mascu… Tatooine 
 5 Obi-Wan Ken…    182    77 auburn, w… fair       blue-gray       57   male  mascu… Stewjon  
 6 Anakin Skyw…    188    84 blond      fair       blue            41.9 male  mascu… Tatooine 
 7 Wilhuff Tar…    180    NA auburn, g… fair       blue            64   male  mascu… Eriadu   
 8 Chewbacca       228   112 brown      unknown    blue           200   male  mascu… Kashyyyk 
 9 Han Solo        180    80 brown      fair       brown           29   male  mascu… Corellia 
10 Greedo          173    74 NA         green      black           44   male  mascu… Rodia    
# … with 54 more rows, and 4 more variables: species <chr>, films <list>, vehicles <list>,
#   starships <list>
  •  Tags:  
  • r
  • Related