Faiz Rasool
2010-Mar-13 19:58 UTC
[R] Two questions, first about contingency tables, and second about table () and data.frame (), from a visually impaired user.
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]]
Tal Galili
2010-Mar-13 20:44 UTC
[R] Two questions, first about contingency tables, and second about table () and data.frame (), from a visually impaired user.
Oh, That's clearer now. You should use two equal vectors when using table. For example: a <- c("a","b","a","b","a","b","a","b") b <- c(1,1,1,1,2,2,2,2) table(a,b) also have a look at ?as.table and ?matrix Does that help ? Tal ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Sat, Mar 13, 2010 at 10:24 PM, Faiz Rasool <faiz7r@gmail.com> wrote:> Hi Tal, > > I am typing: > > >birth =c(50,34)# frequencies of the two categories of the independent > variable. > >mental.health=c(28,26,30)#frequencies of the three categories of > the dependent variable. > >table(birth,mental.health) > I get an error "all arguments must have the same length" > when I use > data.frame(birth,mental.health) > I get "arguments imply differing number of rows." > > > There is no missing value, and the sample size is 84. > > Best regards, > Faiz. > > ----- Original Message ----- > *From:* Tal Galili <tal.galili@gmail.com> > *To:* Faiz Rasool <faiz7r@gmail.com> > *Sent:* Sunday, March 14, 2010 1:04 AM > *Subject:* Re: [R] Two questions, first about contingency tables, and > second about table () and data.frame (), from a visually impaired user. > > Hi Faiz, > > Two ideas: > 1) do you have any NA's ? > 2) can you send an example of the code/ file? That might help people (like > me) to answer. > > Tal > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili@gmail.com | 972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > > ---------------------------------------------------------------------------------------------- > > > > > On Sat, Mar 13, 2010 at 9:58 PM, Faiz Rasool <faiz7r@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@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. >> > >[[alternative HTML version deleted]]
Joshua Wiley
2010-Mar-13 22:35 UTC
[R] Two questions, first about contingency tables, and second about table () and data.frame (), from a visually impaired user.
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), labels=c("Normal","Depressed","Schizophrenic")) table(mental.health,birth) ----------------------- Which should give you: birth 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 ### data.frame(birth,mental.health) ### You might also lookup ?xtabs It is useful for contigency tables, particularly when you have more than 2 variables. I hope that is clear. Best regards, Josh 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 http://www.joshuawiley.com/
Reasonably Related Threads
- Problem in performing goodness of fit test in R.
- How to Save the residuals of an LM object greater or less than a certin value to an R object?
- couple of how-to-do it in R questions regarding corelations and mean and SD of likert items
- Reading results of commands in Microsoft Word typed in the terminal window, A question from a Blind R user.
- How to Save the residuals of an LM object greater or less than a certin value to an R object?