Re: [R] Table and ftable
Try this which gives an object of the required shape and of class c("xtabs", "table") : xx <- xtabs(area ~ sic + level, DF) You can optionally do it like this to make it class "matrix" xx <- xtabs(area ~ sic + level, DF)[] and if you don't want the call attribute: attr(xx, "call") <- NULL On 9/4/07, Giulia Bennati <[EMAIL PROTECTED]> wrote: > Dear listmembers, > I have a little question: I have my data organized as follow > > sic level area > a2112.4 > b3112.3 > b3220.2 > b3220.5 > c1003.0 > c1001.5 > c2421.5 > d2220.2 > > where levels and sics are factors. I'm trying to obtain a matrix like this: > >level > 211311322 100242 222 > sic > a2.4 0 0 0 00 > b 0 2.30.7 0 00 > c 00 0 4.5 1.5 0 > d 00 00 0 0.2 > > I tryed with table function as > table(sic,level) but i obteined only a contingency table. > Have you any suggestions? > Thank you very much, > Giulia > > __ > R-help@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 > and provide commented, minimal, self-contained, reproducible code. > __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Table and ftable
David Barron wrote: > There might be simpler ways, but you can certainly do this with the > reshape package, like this: > > library(reshape) > dta <- read.table("clipboard",header=TRUE) > > sic level area > 1 a 211 2.4 > 2 b 311 2.3 > 3 b 322 0.2 > 4 b 322 0.5 > 5 c 100 3.0 > 6 c 100 1.5 > 7 c 242 1.5 > 8 d 222 0.2 > > > mlt.dta <- melt(dta) > cst.dta <- cast(mlt.dta,sic~level,sum) > > sic 100 211 222 242 311 322 > 1 a NA 2.4 NA NA NA NA > 2 b NA NA NA NA 2.3 0.7 > 3 c 4.5 NA NA 1.5 NA NA > 4 d NA NA 0.2 NA NA NA > > Then just replace the NAs with 0s. > > tapply() will do this too: > with(d,tapply(area,list(sic,level), sum)) 100 211 222 242 311 322 a NA 2.4 NA NA NA NA b NA NA NA NA 2.3 0.7 c 4.5 NA NA 1.5 NA NA d NA NA 0.2 NA NA NA This has the same awkwardness of giving NA for empty cells, and there is no easy way to circumvent it since the FUN of tapply is simply not called for such cells. Replacing NA by zero is a bit dangerous (albeit not in the present case) since you can get an NA cell for more than one reason. A more careful approach is like this: > with(d,{t1 <- tapply(area,list(sic,level), sum) t2 <- table(sic,level) t1[t2==0] <- 0 t1} ) 100 211 222 242 311 322 a 0.0 2.4 0.0 0.0 0.0 0.0 b 0.0 0.0 0.0 0.0 2.3 0.7 c 4.5 0.0 0.0 1.5 0.0 0.0 d 0.0 0.0 0.2 0.0 0.0 0.0 > > HTH. > > David Barron > On 9/4/07, Giulia Bennati <[EMAIL PROTECTED]> wrote: > >> Dear listmembers, >> I have a little question: I have my data organized as follow >> >> sic level area >> a2112.4 >> b3112.3 >> b3220.2 >> b3220.5 >> c1003.0 >> c1001.5 >> c2421.5 >> d2220.2 >> >> where levels and sics are factors. I'm trying to obtain a matrix like this: >> >> level >> 211311322 100242 222 >> sic >> a2.4 0 0 0 00 >> b 0 2.30.7 0 00 >> c 00 0 4.5 1.5 0 >> d 00 00 0 0.2 >> >> I tryed with table function as >> table(sic,level) but i obteined only a contingency table. >> Have you any suggestions? >> Thank you very much, >> Giulia >> >> __ >> R-help@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 >> and provide commented, minimal, self-contained, reproducible code. >> >> > > > -- O__ Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Table and ftable
There might be simpler ways, but you can certainly do this with the reshape package, like this: library(reshape) dta <- read.table("clipboard",header=TRUE) sic level area 1 a 211 2.4 2 b 311 2.3 3 b 322 0.2 4 b 322 0.5 5 c 100 3.0 6 c 100 1.5 7 c 242 1.5 8 d 222 0.2 > mlt.dta <- melt(dta) cst.dta <- cast(mlt.dta,sic~level,sum) sic 100 211 222 242 311 322 1 a NA 2.4 NA NA NA NA 2 b NA NA NA NA 2.3 0.7 3 c 4.5 NA NA 1.5 NA NA 4 d NA NA 0.2 NA NA NA Then just replace the NAs with 0s. HTH. David Barron On 9/4/07, Giulia Bennati <[EMAIL PROTECTED]> wrote: > Dear listmembers, > I have a little question: I have my data organized as follow > > sic level area > a2112.4 > b3112.3 > b3220.2 > b3220.5 > c1003.0 > c1001.5 > c2421.5 > d2220.2 > > where levels and sics are factors. I'm trying to obtain a matrix like this: > > level > 211311322 100242 222 > sic > a2.4 0 0 0 00 > b 0 2.30.7 0 00 > c 00 0 4.5 1.5 0 > d 00 00 0 0.2 > > I tryed with table function as > table(sic,level) but i obteined only a contingency table. > Have you any suggestions? > Thank you very much, > Giulia > > __ > R-help@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 > and provide commented, minimal, self-contained, reproducible code. > -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
[R] Table and ftable
Dear listmembers, I have a little question: I have my data organized as follow sic level area a2112.4 b3112.3 b3220.2 b3220.5 c1003.0 c1001.5 c2421.5 d2220.2 where levels and sics are factors. I'm trying to obtain a matrix like this: level 211311322 100242 222 sic a2.4 0 0 0 00 b 0 2.30.7 0 00 c 00 0 4.5 1.5 0 d 00 00 0 0.2 I tryed with table function as table(sic,level) but i obteined only a contingency table. Have you any suggestions? Thank you very much, Giulia __ R-help@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 and provide commented, minimal, self-contained, reproducible code.