-4

I'm trying to count the number of females, males and N/A in the column 'gender' in the data frame trips. However is is returning me just the number of rows instead.

library(dplyr)
count(trips, vars = "gender")
# A tibble: 1 × 2
#    vars      n
#   <chr>  <int>
#1 gender 286858


head(trips)

trip_id | start_time | stop_time | duration | bike_di | gender | birthyear
 1      | 10:00      | 11:00     |   3600   | BD-215  | Male   | 1960
 1      | 10:30      | 11:00     |   1800   | BD-715  | Female | 1960
 1      | 10:45      | 11:00     |    900   | BD-615  | Male   | 1960
 1      | 10:50      | 11:00     |    600   | BD-315  | Female | 1960
Henrik
  • 65,555
  • 14
  • 143
  • 159
Rochelle Cheema
  • 13
  • 1
  • 1
  • 3

2 Answers2

1

For the females type:

sum(trips$gender=='Female')

For the males type

sum(trips$gender=='male')

For the NA, is it only in the gender column or in the all columns? type:

colsums(is.na(trips))

and you will get number of NAs in each column.

Hopefully it helps.

Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56
Rlearner
  • 93
  • 5
0

Here's a dplyr solution. I added a line where gender is NA as you mentioned you also want to count them.

trips <- read.table(text="trip_id  start_time  stop_time  duration  bike_di  gender  birthyear
 1       10:00       11:00        3600    BD-215   Male    1960
 1       10:30       11:00        1800    BD-715   Female  1960
 1       10:45       11:00         900    BD-615   Male    1960
 1       10:45       11:00         900    BD-615   NA    1960
 1       10:50       11:00         600    BD-315   Female  1960",header=TRUE, stringsAsFactors=FALSE)

 trips %>%
 group_by(gender) %>%
 summarise(gender_count=n())

# A tibble: 3 x 2
  gender gender_count
   <chr>        <int>
1 Female            2
2   Male            2
3   <NA>            1
Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56