[R] Struchture change of a data frame

White.Denis@epamail.epa.gov White.Denis at epamail.epa.gov
Wed May 15 18:42:09 CEST 2002


Beat Huggler wrote:
> I've got an easy question but couldn't find any quick
> solution. I woulk like to change the following matrix
>
>         good bad worse
> Blue       1   2     2
> Yellow     2   1     3
> Black      3   4     4
>
> Into the following structure
>
> good   1 Blue
> Bad    2 Blue
> Worse  2 Blue
> Good   2 Yellow
> Bad    1 Yellow
> Worse  2 Yellow
> Good   2 Black
> Bad    4 Black
> Worse  4 Black

Try this:

R> dat
       good bad worse
blue      1   2     2
yellow    2   1     3
black     3   4     4
R>
R> print (cbind (
+    rep (colnames (dat), 3),
+    as.vector (t (dat)),
+    rep (rownames (dat), 1, each=3)),
+        quote=FALSE)
      [,1]  [,2] [,3]
 [1,] good  1    blue
 [2,] bad   2    blue
 [3,] worse 2    blue
 [4,] good  2    yellow
 [5,] bad   1    yellow
 [6,] worse 3    yellow
 [7,] good  3    black
 [8,] bad   4    black
 [9,] worse 4    black

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list