-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Dear R-helpers, my data.frame is of the form x <- data.frame( f=gl(4,3), X=rep(0:2,4), p=c(.1,.2,.3)) x f X p 1 1 0 0.1 2 1 1 0.2 3 1 2 0.3 4 2 0 0.1 5 2 1 0.2 6 2 2 0.3 7 3 0 0.1 8 3 1 0.2 9 3 2 0.3 10 4 0 0.1 11 4 1 0.2 12 4 2 0.3 which tabulates some values p(X) for several factors f. Now I want to put it in "wide" format, so that factor levels appear as column heads. Note also that X starts from zero. It would be nice if I could simply access p_f[X==0] as f[0]. How can I possibly do that? (The resilting object does not have to be a data.frame. As there are only numeric values, also a matrix would do.) I tried the following y<-unstack(x,form=p~f) row.names(y) <- 0:2 y X1 X2 X3 X4 0 0.1 0.1 0.1 0.1 1 0.2 0.2 0.2 0.2 2 0.3 0.3 0.3 0.3 Now, how to access X3[0], say? Maybe reshape would be the right tool, but I could not figure it out. I appreciate your help. Thanks! -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFEn9G2XjamRUP82DkRAorGAJ9JirG7WtNJLWRQkJvgW0zTFHTYagCgvONw IC4jgoxE2+CsOmmogv5dzF0=24Kj -----END PGP SIGNATURE-----
You need to specify the row/column name as character:> yX1 X2 X3 X4 0 0.1 0.1 0.1 0.1 1 0.2 0.2 0.2 0.2 2 0.3 0.3 0.3 0.3> y[,'X3'][1] 0.1 0.2 0.3> y['0','X3'][1] 0.1 On 6/26/06, Matthias Braeunig <mb.atelier@web.de> wrote:> > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Dear R-helpers, > > my data.frame is of the form > > x <- data.frame( f=gl(4,3), X=rep(0:2,4), p=c(.1,.2,.3)) > x > f X p > 1 1 0 0.1 > 2 1 1 0.2 > 3 1 2 0.3 > 4 2 0 0.1 > 5 2 1 0.2 > 6 2 2 0.3 > 7 3 0 0.1 > 8 3 1 0.2 > 9 3 2 0.3 > 10 4 0 0.1 > 11 4 1 0.2 > 12 4 2 0.3 > > which tabulates some values p(X) for several factors f. > > Now I want to put it in "wide" format, so that factor levels appear as > column heads. Note also that X starts from zero. It would be nice if I > could simply access p_f[X==0] as f[0]. How can I possibly do that? > > (The resilting object does not have to be a data.frame. As there are > only numeric values, also a matrix would do.) > > I tried the following > > y<-unstack(x,form=p~f) > row.names(y) <- 0:2 > y > X1 X2 X3 X4 > 0 0.1 0.1 0.1 0.1 > 1 0.2 0.2 0.2 0.2 > 2 0.3 0.3 0.3 0.3 > > Now, how to access X3[0], say? > > Maybe reshape would be the right tool, but I could not figure it out. > > I appreciate your help. Thanks! > > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2.2 (GNU/Linux) > > iD8DBQFEn9G2XjamRUP82DkRAorGAJ9JirG7WtNJLWRQkJvgW0zTFHTYagCgvONw > IC4jgoxE2+CsOmmogv5dzF0> =24Kj > -----END PGP SIGNATURE----- > > ______________________________________________ > 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 >-- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What is the problem you are trying to solve? [[alternative HTML version deleted]]
You might want to try the Oarray package on CRAN. Andy From: Matthias Braeunig> > Thanks, this is not what what I meant. I need to reshape the > original dataframe that I can access p_f[X] for numerical X. > Maybe I was not clear enough. > > The problem really is that X starts at 0. Note that in my > example changing the row names to 0:2 does not have the > desired effect. > > > > jim holtman wrote: > > You need to specify the row/column name as character: > > > >> y > > X1 X2 X3 X4 > > 0 0.1 0.1 0.1 0.1 > > 1 0.2 0.2 0.2 0.2 > > 2 0.3 0.3 0.3 0.3 > > > >> y[,'X3'] > > [1] 0.1 0.2 0.3 > >> y['0','X3'] > > [1] 0.1 > > > > > > > > > > On 6/26/06, Matthias Braeunig <mb.atelier at web.de> wrote: > >> > > Dear R-helpers, > > > > > > my data.frame is of the form > > > > x <- data.frame( f=gl(4,3), X=rep(0:2,4), p=c(.1,.2,.3)) x > > f X p > > 1 1 0 0.1 > > 2 1 1 0.2 > > 3 1 2 0.3 > > 4 2 0 0.1 > > 5 2 1 0.2 > > 6 2 2 0.3 > > 7 3 0 0.1 > > 8 3 1 0.2 > > 9 3 2 0.3 > > 10 4 0 0.1 > > 11 4 1 0.2 > > 12 4 2 0.3 > > > > which tabulates some values p(X) for several factors f. > > > > Now I want to put it in "wide" format, so that factor > levels appear as > > column heads. Note also that X starts from zero. It would > be nice if I > > could simply access p_f[X==0] as f[0]. How can I possibly do that? > > > > (The resilting object does not have to be a data.frame. As > there are > > only numeric values, also a matrix would do.) > > > > I tried the following > > > > y<-unstack(x,form=p~f) > > row.names(y) <- 0:2 > > y > > X1 X2 X3 X4 > > 0 0.1 0.1 0.1 0.1 > > 1 0.2 0.2 0.2 0.2 > > 2 0.3 0.3 0.3 0.3 > > > > Now, how to access X3[0], say? > > > > Maybe reshape would be the right tool, but I could not > figure it out. > > > > I appreciate your help. Thanks! > > ______________________________________________ > 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 > >