dear all, i'm just about to do some straightforward contingency tables using ftables (and ctab() for percents). the problem: factor "a" are regions, factor "b" are subregions. every region "a" consists of some subregions "b", but obviously not every subregion "b" is part of every region "a". if i use the ftable() function, the table contains a lot of zero rows which i don't want in my output. minimal example: a <- c(1,1,1,1,1,2,2,2,2,2)> b <- c(4,5,6,5,4,7,8,9,8,7) > c <- c("a","b","c","d","a","b","b","a","d","d") > A <- cbind(a,b,c) > Aa b c [1,] "1" "4" "a" [2,] "1" "5" "b" [3,] "1" "6" "c" [4,] "1" "5" "d" [5,] "1" "4" "a" [6,] "2" "7" "b" [7,] "2" "8" "b" [8,] "2" "9" "a" [9,] "2" "8" "d" [10,] "2" "7" "d"> test <- ftable(a,b,c) > testc a b c d a b 1 4 2 0 0 0 5 0 1 0 1 6 0 0 1 0 7 0 0 0 0 8 0 0 0 0 9 0 0 0 0 2 4 0 0 0 0 5 0 0 0 0 6 0 0 0 0 7 0 1 0 1 8 0 1 0 1 9 1 0 0 0 my question: how can i "delete" the zero rows and preserve the structure and attributes of the original table? simply doing something like: test2 <- test[test>0] obviously only returns the non-zero values, but not the nice structure and attributes of the original table. to do it by hand is not an option as the original table has like 2000 rows, 1500 of which are zero... thanks in advance marc ______________________________________________________________ "Hostage" mit Bruce Willis kostenlos anschauen!
Try this: test[test == 0] <- '' test On Tue, Sep 2, 2008 at 1:03 PM, Marc Flockerzi <marc.flockerzi@web.de>wrote:> dear all, > > i'm just about to do some straightforward contingency tables using ftables > (and ctab() for percents). > > the problem: > factor "a" are regions, factor "b" are subregions. > every region "a" consists of some subregions "b", but obviously not every > subregion "b" is part of every region "a". > if i use the ftable() function, the table contains a lot of zero rows which > i don't want in my output. > > minimal example: > > a <- c(1,1,1,1,1,2,2,2,2,2) > > b <- c(4,5,6,5,4,7,8,9,8,7) > > c <- c("a","b","c","d","a","b","b","a","d","d") > > A <- cbind(a,b,c) > > A > a b c > [1,] "1" "4" "a" > [2,] "1" "5" "b" > [3,] "1" "6" "c" > [4,] "1" "5" "d" > [5,] "1" "4" "a" > [6,] "2" "7" "b" > [7,] "2" "8" "b" > [8,] "2" "9" "a" > [9,] "2" "8" "d" > [10,] "2" "7" "d" > > test <- ftable(a,b,c) > > test > c a b c d > a b > 1 4 2 0 0 0 > 5 0 1 0 1 > 6 0 0 1 0 > 7 0 0 0 0 > 8 0 0 0 0 > 9 0 0 0 0 > 2 4 0 0 0 0 > 5 0 0 0 0 > 6 0 0 0 0 > 7 0 1 0 1 > 8 0 1 0 1 > 9 1 0 0 0 > > my question: how can i "delete" the zero rows and preserve the structure > and attributes of the original table? > simply doing something like: > test2 <- test[test>0] > obviously only returns the non-zero values, but not the nice structure and > attributes of the original table. > > to do it by hand is not an option as the original table has like 2000 rows, > 1500 of which are zero... > > thanks in advance > marc > ______________________________________________________________ > "Hostage" mit Bruce Willis kostenlos anschauen! > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
thanks, but not quite what i wanted. to be more precise: i want the whole zero-lines to be deleted, including the attribute. in my final table only rows with non-zero rows should remain. regarding my small toy example: this is what i have:>test> c a b c d > a b > 1 4 2 0 0 0 > 5 0 1 0 1 > 6 0 0 1 0 > 7 0 0 0 0 > 8 0 0 0 0 > 9 0 0 0 0 > 2 4 0 0 0 0 > 5 0 0 0 0 > 6 0 0 0 0 > 7 0 1 0 1 > 8 0 1 0 1 > 9 1 0 0 0this is what i want:> c a b c d > ab > 1 4 2 0 0 0 > 5 0 1 0 1 > 6 0 0 1 0 > 2 7 0 1 0 1 > 8 0 1 0 1 > 9 1 0 0 0best regards marc> -----Urspr?ngliche Nachricht----- > Von: "Henrique Dallazuanna" <wwwhsd at gmail.com> > Gesendet: 02.09.08 18:33:53 > An: "Marc Flockerzi" <marc.flockerzi at web.de> > CC: r-help at r-project.org > Betreff: Re: [R] ftables package, zero rows> > Try this: > > test[test == 0] <- '' > test > > > On Tue, Sep 2, 2008 at 1:03 PM, Marc Flockerzi <marc.flockerzi at web.de> > wrote: > dear all, > > i'm just about to do some straightforward contingency tables using > ftables (and ctab() for percents). > > the problem: > factor "a" are regions, factor "b" are subregions. > every region "a" consists of some subregions "b", but obviously not > every subregion "b" is part of every region "a". > if i use the ftable() function, the table contains a lot of zero > rows which i don't want in my output. > > minimal example: > > a <- c(1,1,1,1,1,2,2,2,2,2) > > b <- c(4,5,6,5,4,7,8,9,8,7) > > c <- c("a","b","c","d","a","b","b","a","d","d") > > A <- cbind(a,b,c) > > A > a b c > [1,] "1" "4" "a" > [2,] "1" "5" "b" > [3,] "1" "6" "c" > [4,] "1" "5" "d" > [5,] "1" "4" "a" > [6,] "2" "7" "b" > [7,] "2" "8" "b" > [8,] "2" "9" "a" > [9,] "2" "8" "d" > [10,] "2" "7" "d" > > test <- ftable(a,b,c) > > test > c a b c d > a b > 1 4 2 0 0 0 > 5 0 1 0 1 > 6 0 0 1 0 > 7 0 0 0 0 > 8 0 0 0 0 > 9 0 0 0 0 > 2 4 0 0 0 0 > 5 0 0 0 0 > 6 0 0 0 0 > 7 0 1 0 1 > 8 0 1 0 1 > 9 1 0 0 0 > > my question: how can i "delete" the zero rows and preserve the > structure and attributes of the original table? > simply doing something like: > test2 <- test[test>0] > obviously only returns the non-zero values, but not the nice > structure and attributes of the original table. > > to do it by hand is not an option as the original table has like > 2000 rows, 1500 of which are zero... > > thanks in advance > marc > ______________________________________________________________ > "Hostage" mit Bruce Willis kostenlos anschauen! > > ______________________________________________ > 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. > > > > -- > Henrique Dallazuanna > Curitiba-Paran?-Brasil > 25? 25' 40" S 49? 16' 22" O > >______________________________________________________________ "Hostage" mit Bruce Willis kostenlos anschauen!
Does this help? a <- c(1,1,1,1,1,2,2,2,2,2) b <- c(4,5,6,5,4,7,8,9,8,7) c <- c("a","b","c","d","a","b","b","a","d","d") A <- cbind(a,b,c) test <- ftable(a,b,c) test.df <- data.frame(test) test.df[test.df$Freq != 0, ] Doesn't quite give the layout you seem to want, but effectively removes the zeros. Mikkel