0

I am taking Datacamp's Data Analysis and Statistical Inference. The following is code to load related to the Global Index of Religiosity And Atheism and subsetting the US 2012 data.

load(url("http://s3.amazonaws.com/assets.datacamp.com/course/dasi/atheism.RData"))
us12 = subset(atheism, atheism$nationality == "United States" & atheism$year =="2012")

My question is why do I see the other countries when I type

table(us12)

Is there a way I can subset only the filtered data?

Thanks.

talat
  • 68,970
  • 21
  • 126
  • 157
chribonn
  • 445
  • 5
  • 20
  • FYI, when using the `subset` convenience function, you dont need to type `atheism$` inside it. Just access the variables directly, i.e. `subset(atheism, nationality == "United States" & year =="2012")` – talat Dec 19 '14 at 09:21
  • When you subset the data frame the factor variables keep all the levels, so the problem is dropping unused factor levels after subsetting, which is [answered here](http://stackoverflow.com/questions/1195826/dropping-factor-levels-in-a-subsetted-data-frame-in-r): `us12$nationality <- factor(us12$nationality)` and `us12$response <- factor(us12$response)` should fix it. – andybega Dec 19 '14 at 09:28

1 Answers1

3

Because us12 is a data.frame in which nationality and response columns are factors, including all the levels of the original data.frame.

In fact, table(us12) returns you the counts, and as you can see, all the counts for nationalities different from United States are zero.

If this is a problem for you, you can use the droplevels function, that removes the levels actually not present in the data.frame:

> us12dropped=droplevels(us12)
> table(us12dropped)
, , year = 2012

               response
nationality     atheist non-atheist
  United States      50         952
WoDoSc
  • 2,598
  • 1
  • 13
  • 26
  • 3
    (+1) But you should add a hint to `droplevels`. – Roland Dec 19 '14 at 09:26
  • you can also do `table(us12,exclude=setdiff(levels(us12$nationality),"United States"))` but it's not as simple as using `droplevels` – Cath Dec 19 '14 at 09:42