Ravi Varadhan
2005-Apr-20 15:38 UTC
[R] Keeping factors with zero occurrences in "table" output
Dear R group, I have a data frame which contains data on preferences on 7 items (ranks 1 through 7) listed by each participant. I would like to tabulate this in a 7x7 table where the rows would be the items and the columns would be the number of times that item received a particular rank. I tried doing this by creating a matrix by "rbind"ing each vector obtained using "table" on each item. This, however, does not work because there are some items which didn't receive some of the ranks, so "table" does not list that item. Is there a way to force it to list the item and give a "0" to it? Or, is there a simpler way to solve this problem? Thanks for any help, Ravi. -------------------------------------------------------------------------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: <mailto:rvaradhan@jhmi.edu> rvaradhan@jhmi.edu -------------------------------------------------------------------------- [[alternative HTML version deleted]]
Achim Zeileis
2005-Apr-20 16:12 UTC
[R] Keeping factors with zero occurrences in "table" output
On Wed, 20 Apr 2005 11:38:13 -0400 Ravi Varadhan wrote:> Dear R group, > > > > I have a data frame which contains data on preferences on 7 items > (ranks 1 through 7) listed by each participant. I would like to > tabulate this in a 7x7 table where the rows would be the items and the > columns would be the number of times that item received a particular > rank. > > I tried doing this by creating a matrix by "rbind"ing each vector > obtained using "table" on each item. This, however, does not work > because there are some items which didn't receive some of the ranks, > so "table" does not list that item. Is there a way to force it to > list the item and give a "0" to it? Or, is there a simpler way to > solve this problem?I think the cleanest way is to store the preferences as a "factor" and tell R at the creation of that factor what the levels are, i.e. preference <- factor(preference, levels = 1:7) Then calling table(preference) will also report categories with 0 observations. Z> > > Thanks for any help, > > Ravi. > > > > --------------------------------------------------------------------- > ----- > > Ravi Varadhan, Ph.D. > > Assistant Professor, The Center on Aging and Health > > Division of Geriatric Medicine and Gerontology > > Johns Hopkins University > > Ph: (410) 502-2619 > > Fax: (410) 614-9625 > > Email: <mailto:rvaradhan at jhmi.edu> rvaradhan at jhmi.edu > > --------------------------------------------------------------------- > ----- > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Wiener, Matthew
2005-Apr-20 16:15 UTC
[R] Keeping factors with zero occurrences in "table" output
Ravi -- If you use table on a factor, you'll get 0's if appropriate:> table(sample(1:5, 10, replace = TRUE)) #no 2's, by chance1 3 4 5 3 1 2 4> table(sample(1:5, 20, replace = TRUE)) # no 6's here, so 6 doesn't showup 1 2 3 4 5 3 5 2 5 5 ## now make it a factor, and you get 0 3's and 0 6's> table(factor(sample(1:5, 20, replace = TRUE), levels = 1:6))1 2 3 4 5 6 5 4 0 1 10 0 ## just happened to get no 3's in previous sample; it doesn't happen every time:> table(factor(sample(1:5, 20, replace = TRUE), levels = 1:6))1 2 3 4 5 6 7 3 3 5 2 0 You can probably create the factor you want with "interaction" on participants and ranks. Hope this helps, Matt Wiener -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ravi Varadhan Sent: Wednesday, April 20, 2005 11:38 AM To: r-help at stat.math.ethz.ch Subject: [R] Keeping factors with zero occurrences in "table" output Dear R group, I have a data frame which contains data on preferences on 7 items (ranks 1 through 7) listed by each participant. I would like to tabulate this in a 7x7 table where the rows would be the items and the columns would be the number of times that item received a particular rank. I tried doing this by creating a matrix by "rbind"ing each vector obtained using "table" on each item. This, however, does not work because there are some items which didn't receive some of the ranks, so "table" does not list that item. Is there a way to force it to list the item and give a "0" to it? Or, is there a simpler way to solve this problem? Thanks for any help, Ravi. -------------------------------------------------------------------------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: <mailto:rvaradhan at jhmi.edu> rvaradhan at jhmi.edu -------------------------------------------------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Liaw, Andy
2005-Apr-20 16:24 UTC
[R] Keeping factors with zero occurrences in "table" output
If you make them into factors, the empty ones should show up:> f1 <- sample(1:7, 10, replace=TRUE) > f2 <- sample(1:6, 10, replace=TRUE) > table(f1, f2)f2 f1 1 2 3 5 6 1 0 0 1 0 0 2 0 0 1 0 0 3 0 0 2 1 0 4 0 1 0 0 1 5 1 1 0 0 0 7 0 0 0 1 0> ff1 <- factor(f1, levels=1:7) > ff2 <- factor(f2, levels=1:7) > table(ff1, ff2)ff2 ff1 1 2 3 4 5 6 7 1 0 0 1 0 0 0 0 2 0 0 1 0 0 0 0 3 0 0 2 0 1 0 0 4 0 1 0 0 0 1 0 5 1 1 0 0 0 0 0 6 0 0 0 0 0 0 0 7 0 0 0 0 1 0 0 HTH, Andy> From: Ravi Varadhan > > Dear R group, > > > > I have a data frame which contains data on preferences on 7 > items (ranks 1 > through 7) listed by each participant. I would like to > tabulate this in a > 7x7 table where the rows would be the items and the columns > would be the > number of times that item received a particular rank. > > > > I tried doing this by creating a matrix by "rbind"ing each > vector obtained > using "table" on each item. This, however, does not work > because there are > some items which didn't receive some of the ranks, so "table" > does not list > that item. Is there a way to force it to list the item and > give a "0" to > it? Or, is there a simpler way to solve this problem? > > > > Thanks for any help, > > Ravi. > > > > -------------------------------------------------------------- > ------------ > > Ravi Varadhan, Ph.D. > > Assistant Professor, The Center on Aging and Health > > Division of Geriatric Medicine and Gerontology > > Johns Hopkins University > > Ph: (410) 502-2619 > > Fax: (410) 614-9625 > > Email: <mailto:rvaradhan at jhmi.edu> rvaradhan at jhmi.edu > > -------------------------------------------------------------- > ------------ > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >