7

Here is my data

> a
 [1] Male   Male   Female Male   Male   Male   Female Female Male   Male   Female Male   Male   Male  
[15] Female Female Female Male   Female Male   Female Male   Male   Female Male   Male   Female Male  
[29] Male   Male   Female Male   Male   Male   Female Female Male   Male   Male   Male   Male  
Levels:  Female Male

> b
[1] 0 1 0 1 0 0 0 0 1 1 1 1 0 1 0 0 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 1 1 0 0 1 0 1 1 1 1
Levels: 0 1

> table(a,b)
        b
a         0  1
          0  0
  Female 10  4
  Male   12 15

I don't know why the result of table(a,b) has a row of (0 0), my expected result is as follows:

> table(a,b)
        b
a         0  1
  Female 10  4
  Male   12 15

Could you tell me why this happens and how to correct it, thank you!

> dput(a)
structure(c(3L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 
3L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 
3L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("", 
"Female", "Male"), class = "factor")

> dput(b)
structure(c(1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L), .Label = c("0", 
"1"), class = "factor")
epo3
  • 2,991
  • 2
  • 33
  • 60
lightsnail
  • 788
  • 5
  • 20
  • Are the length of `a` and `b` the same? – Psidom Jul 05 '16 at 23:00
  • 8
    you may have emptty levels of a ie `""`. Try `nlevels(a)` – user20650 Jul 05 '16 at 23:04
  • @Psidom yes, the length of `a` and `b` is the same – lightsnail Jul 05 '16 at 23:05
  • 3
    Based on the extra space in `Levels:__Female Male`, I think @user20650 is right. If there were only two levels, it would be `Levels:_Female Male` (only one space, underscores are spaces). – r2evans Jul 05 '16 at 23:05
  • As commented above, you may have an empty string in your vector `a`. Check that out. – Psidom Jul 05 '16 at 23:07
  • 1
    @Psidom; it may not be an empty string, but an extra level – user20650 Jul 05 '16 at 23:08
  • 1
    Essentially the same issue as here - http://stackoverflow.com/questions/32981564/barplots-in-r-strange-empty-1st-column/32981755 – thelatemail Jul 05 '16 at 23:09
  • @user20650 Yeah, you got it. It does have an extra level there. – Psidom Jul 05 '16 at 23:10
  • 2
    As you can see, you have three levels (`.Label`) in your `dput`. Fix your data entry problem and you'll fix the table. (Or just reassign the levels using, oddly enough, `levels(a) <- c("Female", "Male")`.) – r2evans Jul 05 '16 at 23:10
  • 2
    Or use the droplevels command: `a<-droplevels(a)` – Dave2e Jul 05 '16 at 23:20
  • Thank all of you! @user20650 @Gregor @r2evans @Psidom The original data is in a ".csv" form, then I used `read.csv("mydata.csv", header=T)` to read the data. And the list `a` is just one column of the input data frame, I used `dataframe$a` to extract that column, and there are no blanks in that column, a little weird. How came the `" "` level? – lightsnail Jul 05 '16 at 23:35
  • 2
    This will work with the example data: `table(as.character(a), b, useNA="no")`. Factors are always a little weird. I always try to use as=TRUE in my `read.` family of functions which converts variables to character rather than factor. – lmo Jul 05 '16 at 23:41
  • @Imo Yes, it works. Thank you! – lightsnail Jul 05 '16 at 23:45

1 Answers1

1

From the comments above:

This is happening because there is an empty factor level in a:

> levels(a)
[1] ""       "Female" "Male" 

You can keep produce a table that disregards empty factor levels (from @lmo's comment):

table(as.character(a), b)

Alternatively, you can easily remove factor levels without observations (from @Dave2e's comment)

a <- droplevels(a)
table(a, b)
KenHBS
  • 6,756
  • 6
  • 37
  • 52