user12310746
user12310746

Reputation: 279

In R, how do I count the number of occurrences for a specific column?

I imported a SAS dataset into RStudio. I want to look at a specific column called "sex" (1 = female, 2 = male) and then count the number of 1s and 2s, respectively, but I'm getting the following error message:

Error message

> colnames(bios)
  [1] "PUF_CASE_ID"                 "PUF_FACILITY_ID"            
  [3] "FACILITY_TYPE_CD"            "FACILITY_LOCATION_CD"       
  [5] "AGE"                         "SEX"                        
  [7] "RACE"                        "SPANISH_HISPANIC_ORIGIN"    



  PUF_CASE_ID PUF_FACILITY_ID FACILITY_TYPE_CD FACILITY_LOCATI…   AGE   SEX  RACE
   <chr>       <chr>                      <dbl>            <dbl> <dbl> <dbl> <dbl>
 1 Db309d6d8-… OGMJIMFFRC                     2                1    82     2     1
 2 D39a0df19-… OGMJIMFFRC                     2                1    68     1     1
 3 D40032d28-… OGMJIMFFRC                     2                1    76     1     1
 4 D2dac989c-… OGMJIMFFRC                     2                1    82     1     1
 5 Db0ba85a6-… OGMJIMFFRC                     2                1    64     1     1
 6 D9448c7ff-… OGMJIMFFRC                     2                1    55     1     1
 7 Daa3e4e44-… OGMJIMFFRC                     2                1    50     2     1
 8 D5f0e487d-… OGMJIMFFRC                     2                1    58     2     3
 9 D353b0fac-… OGMJIMFFRC                     2                1    80     1     1
10 D1d1761fb-… OGMJIMFFRC                     2                1    71     1     1

I'm a total beginner in R and am not sure what went wrong. I can view my dataset, and I'm definitely not spelling the column names incorrectly.

Would appreciate any help!

Upvotes: 0

Views: 204

Answers (2)

Kay
Kay

Reputation: 2332

Your column names are in uppercase This should work in base R

table(bios[,'SEX'])

If you're interested in the dplyr approach, do this

library(tidyverse)
bios%>%select(SEX)

To count the number of 1s and 2s, do this

bios%>%count(SEX)

Upvotes: 2

dc37
dc37

Reputation: 16178

Without having to install any packages, in base R, you can do simply:

summary(as.factor(bios$SEX))

or using the function table:

table(as.factor(bios$SEX))

Upvotes: 1

Related Questions