See ?reshape Uwe Ligges Achaz von Hardenberg wrote:> Hi, > I have a data.frame which looks like this: > > id est0 est1 est2 est3 est4 est5 est6 est7 > 1 1 2 3 1 7 9 3 4 > 2 4 1 1 7 6 5 1 2 > [...] > > > I would like to reorder it to obtain the following: > > id est VALUE > 1 0 1 > 1 1 2 > 1 2 3 > 1 3 1 > 1 4 7 > 1 5 9 > 1 6 3 > 1 7 4 > 2 0 4 > 2 1 1 > 2 2 1 > 2 3 7 > 2 4 6 > 2 5 5 > 2 6 1 > 2 7 2 > [...] > > Can anybody help me out? > > Thanks in advance! > > Achaz > > > Achaz von Hardenberg > -------------------------------------------------------------------------------------- > Centro Studi Fauna Alpina - Alpine Wildlife Research Centre > Parco Nazionale Gran Paradiso, via della Rocca 47, 10123 Torino, Italy > > e-mail: fauna at pngp.it > Tel. (office): +39.011.8606212 > Tel. (mobile): +39.328.8736291 > Fax: +39.011.8121305 > -------------------------------------------------------------------------------------- > Open access to all papers published in the Journal of Mountain Ecology: > http://www.mountainecology.org > > GSE-AIESG (Gruppo Stambecco Europa - Alpine Ibex European Specialist Group): > http://www.gseonline.org > > ______________________________________________ > 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
Use reshape(), as:> datid est0 est1 est2 est3 est4 est5 est6 est7 1 1 1 2 3 1 7 9 3 4 2 2 4 1 1 7 6 5 1 2> dat2 <- reshape(dat, varying=list(names(dat)[-1]), times=0:7,direction="long")> dat2id time est0 1.0 1 0 1 2.0 2 0 4 1.1 1 1 2 2.1 2 1 1 1.2 1 2 3 2.2 2 2 1 1.3 1 3 1 2.3 2 3 7 1.4 1 4 7 2.4 2 4 6 1.5 1 5 9 2.5 2 5 5 1.6 1 6 3 2.6 2 6 1 1.7 1 7 4 2.7 2 7 2 Andy> From: Achaz von Hardenberg > > Hi, > I have a data.frame which looks like this: > > id est0 est1 est2 est3 est4 est5 est6 est7 > 1 1 2 3 1 7 9 3 4 > 2 4 1 1 7 6 5 1 2 > [...] > > > I would like to reorder it to obtain the following: > > id est VALUE > 1 0 1 > 1 1 2 > 1 2 3 > 1 3 1 > 1 4 7 > 1 5 9 > 1 6 3 > 1 7 4 > 2 0 4 > 2 1 1 > 2 2 1 > 2 3 7 > 2 4 6 > 2 5 5 > 2 6 1 > 2 7 2 > [...] > > Can anybody help me out? > > Thanks in advance! > > Achaz > > > Achaz von Hardenberg > -------------------------------------------------------------- > ------------------------ > Centro Studi Fauna Alpina - Alpine Wildlife Research Centre > Parco Nazionale Gran Paradiso, via della Rocca 47, 10123 Torino, Italy > > e-mail: fauna at pngp.it > Tel. (office): +39.011.8606212 > Tel. (mobile): +39.328.8736291 > Fax: +39.011.8121305 > -------------------------------------------------------------- > ------------------------ > Open access to all papers published in the Journal of > Mountain Ecology: > http://www.mountainecology.org > > GSE-AIESG (Gruppo Stambecco Europa - Alpine Ibex European > Specialist Group): > http://www.gseonline.org > > ______________________________________________ > 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 > > >
Hi R> d <- data.frame(id=1:3,est0=c(1,10,100),est1=c(6,7,8),est2=c(-5,-5,-5)) R> d id est0 est1 est2 1 1 1 6 -5 2 2 10 7 -5 3 3 100 8 -5 R> out <- as.vector(t(d[,-1])) R> cbind(id=rep(d$id,each=nrow(d)),est=as.vector(row(as.matrix(d[, -1]))),out) id est out [1,] 1 1 1 [2,] 1 2 6 [3,] 1 3 -5 [4,] 2 1 10 [5,] 2 2 7 [6,] 2 3 -5 [7,] 3 1 100 [8,] 3 2 8 [9,] 3 3 -5 R> Hope this helps rksh On May 6, 2005, at 02:26 pm, Achaz von Hardenberg wrote:> Hi, > I have a data.frame which looks like this: > > id est0 est1 est2 est3 est4 est5 est6 est7 > 1 1 2 3 1 7 9 3 4 > 2 4 1 1 7 6 5 1 2 > [...] > > > I would like to reorder it to obtain the following: > > id est VALUE > 1 0 1 > 1 1 2 > 1 2 3 > 1 3 1 > 1 4 7 > 1 5 9 > 1 6 3 > 1 7 4 > 2 0 4 > 2 1 1 > 2 2 1 > 2 3 7 > 2 4 6 > 2 5 5 > 2 6 1 > 2 7 2 > [...] > > Can anybody help me out? > > Thanks in advance! > > Achaz > > > Achaz von Hardenberg > ----------------------------------------------------------------------- > --------------- > Centro Studi Fauna Alpina - Alpine Wildlife Research Centre > Parco Nazionale Gran Paradiso, via della Rocca 47, 10123 Torino, Italy > > e-mail: fauna at pngp.it > Tel. (office): +39.011.8606212 > Tel. (mobile): +39.328.8736291 > Fax: +39.011.8121305 > ----------------------------------------------------------------------- > --------------- > Open access to all papers published in the Journal of Mountain Ecology: > http://www.mountainecology.org > > GSE-AIESG (Gruppo Stambecco Europa - Alpine Ibex European Specialist > Group): > http://www.gseonline.org > > ______________________________________________ > 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 > >-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
On 5/5/05, Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:> See ?reshape > > Uwe Ligges > > > Achaz von Hardenberg wrote: > > > Hi, > > I have a data.frame which looks like this: > > > > id est0 est1 est2 est3 est4 est5 est6 est7 > > 1 1 2 3 1 7 9 3 4 > > 2 4 1 1 7 6 5 1 2 > > [...] > > > > > > I would like to reorder it to obtain the following: > > > > id est VALUE > > 1 0 1 > > 1 1 2 > > 1 2 3 > > 1 3 1 > > 1 4 7 > > 1 5 9 > > 1 6 3 > > 1 7 4 > > 2 0 4 > > 2 1 1 > > 2 2 1 > > 2 3 7 > > 2 4 6 > > 2 5 5 > > 2 6 1 > > 2 7 2 > > [...]I would probably use reshape as others have already pointed out but just for the fun of it note that if you convert it from a data frame to a matrix to a table to a data frame in this order: data.frame ==> matrix ==> table ==> data.frame We can get the result by using matrix() followed by as.data.frame.table() (which converts it to table and then data frame all in one). The one feature of this solution is that each step is relatively simple. I assume the row and column ordering is important, given the Subject, but if it is not then the second line can be simplified to just: as.data.frame.table(mat, resp = "VALUE")> mat <- matrix(unlist(dat[,-1]), nrow(dat), dimnames = list(id = dat$id, est = 0:7)) > as.data.frame.table(t(mat), resp = "VALUE")[c(2:1,3)]id est VALUE 1 1 0 1 2 1 1 2 3 1 2 3 4 1 3 1 5 1 4 7 6 1 5 9 7 1 6 3 8 1 7 4 9 2 0 4 10 2 1 1 11 2 2 1 12 2 3 7 13 2 4 6 14 2 5 5 15 2 6 1 16 2 7 2
Hi, I have a data.frame which looks like this: id est0 est1 est2 est3 est4 est5 est6 est7 1 1 2 3 1 7 9 3 4 2 4 1 1 7 6 5 1 2 [...] I would like to reorder it to obtain the following: id est VALUE 1 0 1 1 1 2 1 2 3 1 3 1 1 4 7 1 5 9 1 6 3 1 7 4 2 0 4 2 1 1 2 2 1 2 3 7 2 4 6 2 5 5 2 6 1 2 7 2 [...] Can anybody help me out? Thanks in advance! Achaz Achaz von Hardenberg -------------------------------------------------------------------------------------- Centro Studi Fauna Alpina - Alpine Wildlife Research Centre Parco Nazionale Gran Paradiso, via della Rocca 47, 10123 Torino, Italy e-mail: fauna at pngp.it Tel. (office): +39.011.8606212 Tel. (mobile): +39.328.8736291 Fax: +39.011.8121305 -------------------------------------------------------------------------------------- Open access to all papers published in the Journal of Mountain Ecology: http://www.mountainecology.org GSE-AIESG (Gruppo Stambecco Europa - Alpine Ibex European Specialist Group): http://www.gseonline.org