markleeds at verizon.net
2008-Jun-04 19:20 UTC
[R] converting a table to a dataframe or a matrix
can someone show me how to convert a table to a data.frame or a matrix ? I tried below and as.data.frame rearranges the columns similarly to a melt from reshape and as.matrix didn't change it. I actually would prefer to change it to a dataframe but if someone can show me how to convert it to a matrix, then as.data.frame will work on that. I was thinking about just changing the class to matrix since table looks very similar to a matrix but I wasn't sure if that would cause side injuries. thanks. temp <- structure(c(0.345901639344262, 0.360117302052786, 0.354304635761589, 0.354319180087848, 0.365524402907580, 0.359455958549223, 0.340284360189573, 0.369763205828780, 0.304918032786885, 0.304985337243402, 0.310596026490066, 0.287457296242069, 0.285565939771547, 0.264248704663212, 0.297630331753555, 0.231329690346084, 0.349180327868852, 0.334897360703812, 0.335099337748344, 0.358223523670083, 0.348909657320872, 0.376295336787565, 0.362085308056872, 0.398907103825137), class = "table", .Dim = c(8L, 3L), .Dimnames = structure(list( c("(-1,-0.75]", "(-0.75,-0.5]", "(-0.5,-0.25]", "(-0.25,0]", "(0,0.25]", "(0.25,0.5]", "(0.5,0.75]", "(0.75,1]"), c("0", "1", "2")), .Names = c("", ""))) print(temp) tempa <- as.data.frame(temp) tempb <- as.matrix(temp) print(str(tempa)) print(str(tempb))
Henrique Dallazuanna
2008-Jun-04 19:39 UTC
[R] converting a table to a dataframe or a matrix
Hi Mark, Try this: as.data.frame.matrix(temp) or as.data.frame(unclass(temp)) On Wed, Jun 4, 2008 at 4:20 PM, <markleeds@verizon.net> wrote:> > can someone show me how to convert a table to a data.frame or a matrix ? I > tried below and as.data.frame rearranges the columns > similarly to a melt from reshape and as.matrix didn't change it. I actually > would prefer to change it to a dataframe but if someone > can show me how to convert it to a matrix, then as.data.frame will work on > that. I was thinking about just changing the class to matrix > since table looks very similar to a matrix but I wasn't sure if that would > cause side injuries. thanks. > > temp <- structure(c(0.345901639344262, 0.360117302052786, > 0.354304635761589, > 0.354319180087848, 0.365524402907580, 0.359455958549223, 0.340284360189573, > 0.369763205828780, 0.304918032786885, 0.304985337243402, 0.310596026490066, > 0.287457296242069, 0.285565939771547, 0.264248704663212, 0.297630331753555, > 0.231329690346084, 0.349180327868852, 0.334897360703812, 0.335099337748344, > 0.358223523670083, 0.348909657320872, 0.376295336787565, 0.362085308056872, > 0.398907103825137), class = "table", .Dim = c(8L, 3L), .Dimnames > structure(list( > c("(-1,-0.75]", "(-0.75,-0.5]", "(-0.5,-0.25]", "(-0.25,0]", > "(0,0.25]", "(0.25,0.5]", "(0.5,0.75]", "(0.75,1]"), c("0", > "1", "2")), .Names = c("", ""))) > > print(temp) > > tempa <- as.data.frame(temp) > tempb <- as.matrix(temp) > > print(str(tempa)) > print(str(tempb)) > > ______________________________________________ > 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]]
Gabor Grothendieck
2008-Jun-04 19:39 UTC
[R] converting a table to a dataframe or a matrix
Either of these will do it: head(temp, Inf) class(temp) <- "matrix" On Wed, Jun 4, 2008 at 3:20 PM, <markleeds at verizon.net> wrote:> > can someone show me how to convert a table to a data.frame or a matrix ? I > tried below and as.data.frame rearranges the columns > similarly to a melt from reshape and as.matrix didn't change it. I actually > would prefer to change it to a dataframe but if someone > can show me how to convert it to a matrix, then as.data.frame will work on > that. I was thinking about just changing the class to matrix > since table looks very similar to a matrix but I wasn't sure if that would > cause side injuries. thanks. > > temp <- structure(c(0.345901639344262, 0.360117302052786, 0.354304635761589, > 0.354319180087848, 0.365524402907580, 0.359455958549223, 0.340284360189573, > 0.369763205828780, 0.304918032786885, 0.304985337243402, 0.310596026490066, > 0.287457296242069, 0.285565939771547, 0.264248704663212, 0.297630331753555, > 0.231329690346084, 0.349180327868852, 0.334897360703812, 0.335099337748344, > 0.358223523670083, 0.348909657320872, 0.376295336787565, 0.362085308056872, > 0.398907103825137), class = "table", .Dim = c(8L, 3L), .Dimnames > structure(list( > c("(-1,-0.75]", "(-0.75,-0.5]", "(-0.5,-0.25]", "(-0.25,0]", > "(0,0.25]", "(0.25,0.5]", "(0.5,0.75]", "(0.75,1]"), c("0", > "1", "2")), .Names = c("", ""))) > > print(temp) > > tempa <- as.data.frame(temp) > tempb <- as.matrix(temp) > > print(str(tempa)) > print(str(tempb)) > > ______________________________________________ > 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. >