[R] Two questions, first about contingency tables, and second about table () and data.frame (), from a visually impaired user.

Joshua Wiley jwiley.psych at gmail.com
Sat Mar 13 23:35:26 CET 2010

Dear Faiz,

I believe that your basic issue is that you are trying to use
frequencies directly.  table() needs all the arguments to be of the
same length, because it counts the frequencies from raw data.  So for
two variables, you need pairs of scores indicating whether there was a
birth complication or not and the category of mental health.

Coding birth as 0,1 for whether a complication is present (1) or absent (0)
Coding mental health as 1=Normal, 2=Depressed, 3=Schizophrenic
Here is an example:

birth <- c(rep(1,50),rep(0,34)) # using rep() to create 50 1s and 34 0s, etc.
birth <- factor(birth, levels=c(1,0), labels=c("Birth
Complications","No Birth Complications"))
mental.health <- c(rep(1,30),rep(2,26),rep(3,28))
mental.health <- factor(mental.health, levels=c(1,2,3),

Which should give you:

mental.health   Birth Complications No Birth Complications
  Normal                         30                      0
  Depressed                      20                      6
  Schizophrenic                   0                     28

You do not need to use factor(), the table would still work, they just
wouldn't have the nice names.  You can also put these variables into a
data frame


You might also lookup


It is useful for contigency tables, particularly when you have more
than 2 variables.

I hope that is clear.

Best regards,


On Sat, Mar 13, 2010 at 11:58 AM, Faiz Rasool <faiz7r at gmail.com> wrote:
> Hi all,
> I want  to make a contingency table in R. I want to tabulate two variables, one as the independent and second as the dependent variable. The IV has two categories, namely, birth complications, and no birth complications. The frequency of birth complication category is fifty, and the frequency of no birth complication category is 34. The categories and frequencies of DV follows. Schizophrenic 28, depressed 26, normal 30. When I am trying to make a contingency table in R, using table(name of variable one,name of variable two), I am getting an error that all arguments must have the same length. I believe that there would be two rows and three columns according to categories of IV and DV, But I guess R wants a third row for IV. When I am trying my luck with data.frame(var1,var2), I receive an error "arguments imply differing number of rows". Any suggestion on how I can make a contingency table using the data above?
> My second question is a result of my inability to see the screen. I want to know that what is the difference between the tables you can make using table () and data.frame (). When I think of a table in my mind, I think of horizontal rows and vertical columns presenting data on different variables. But I  am not sure what type of tables data.frame () prints on the screen and what type of tables table () prints on the screen. and which function should I use when I want to make  tables you are suppose to make in statistics.
> Thank you all, and sorry for such basic questions.
> faiz.
>        [[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Joshua Wiley
Senior in Psychology
University of California, Riverside

More information about the R-help mailing list