Hello R-user community! I am running R 2.7.0 on a Power Book (Tiger). (I am still R and statistics beginner) I have a problem that is certainly solved very easily but presently I don`t know how. I have a dataframe and two of my variables are in the wrong position and I would like to swap those variables. It is easier to handle the dataset if the variables have a certain order. I will be grateful for a hint to solve this. Greets Birgit Birgit Lemcke Institut f?r Systematische Botanik Zollikerstrasse 107 CH-8008 Z?rich Switzerland Ph: +41 (0)44 634 8351 birgit.lemcke at systbot.uzh.ch 175 Jahre UZH ?staunen.erleben.begreifen. Naturwissenschaft zum Anfassen.? Weitere Informationen http://www.175jahre.uzh.ch/naturwissenschaft
Birgit Lemcke:> I have a dataframe and two of my variables are in the wrong position > and I would like to swap those variables.To swap column 1 and 2, try d[c(1,2)]=d[c(2,1)] Note that this is different from d[,c(1,2)]=d[,c(2,1)] which will swap the data, but not the column names. -- Karl Ove Hufthammer
Thanks but it swaps in both cases only the data: FemMal_88[c(61,62)]=FemMal_88[c(62,61)] FemMal_88[,c(61,62)]=FemMal_88[,c(62,61)] Greets B. Karl Ove Hufthammer-4 wrote:> > Birgit Lemcke: > >> I have a dataframe and two of my variables are in the wrong position >> and I would like to swap those variables. > > To swap column 1 and 2, try > > d[c(1,2)]=d[c(2,1)] > > Note that this is different from > > d[,c(1,2)]=d[,c(2,1)] > > which will swap the data, but not the column names. > > -- > Karl Ove Hufthammer > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Swap-variables-in-data.frame-tp17597476p17598708.html Sent from the R help mailing list archive at Nabble.com.
On Mon, Jun 2, 2008 at 11:59 AM, Blubbele <birgit.lemcke at systbot.uzh.ch> wrote:> > Thanks but it swaps in both cases only the data: > > FemMal_88[c(61,62)]=FemMal_88[c(62,61)] > > > FemMal_88[,c(61,62)]=FemMal_88[,c(62,61)]The following works: d <- data.frame(a=c(1,2),b=c(3,4)) d <- d[,c(2,1)] Paul
Thanks Paul. I am not sure if I understood well, but when I do it then I have only two columns left:> L3 <- LETTERS[1:3] > (d <- data.frame(cbind(x=1, y=1:10, z=11:20), fac=sample(L3, 10, > replace=TRUE)))x y z fac 1 1 1 11 C 2 1 2 12 B 3 1 3 13 B 4 1 4 14 C 5 1 5 15 C 6 1 6 16 B 7 1 7 17 C 8 1 8 18 C 9 1 9 19 B 10 1 10 20 C> d <- d[,c(2,1)] > dy x 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 6 1 7 7 1 8 8 1 9 9 1 10 10 1 But I have more than two columns in my data.frame. Birgit Paul Smith wrote:> > On Mon, Jun 2, 2008 at 11:59 AM, Blubbele <birgit.lemcke at systbot.uzh.ch> > wrote: >> >> Thanks but it swaps in both cases only the data: >> >> FemMal_88[c(61,62)]=FemMal_88[c(62,61)] >> >> >> FemMal_88[,c(61,62)]=FemMal_88[,c(62,61)] > > The following works: > > d <- data.frame(a=c(1,2),b=c(3,4)) > d <- d[,c(2,1)] > > Paul > > ______________________________________________ > 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. > >----- The art of living is more like wrestling than dancing. (Marcus Aurelius) -- View this message in context: http://www.nabble.com/Swap-variables-in-data.frame-tp17597476p17599719.html Sent from the R help mailing list archive at Nabble.com.
On Mon, Jun 2, 2008 at 1:04 PM, Birgitle <birgit.lemcke at systbot.uzh.ch> wrote:> > Thanks Paul. > > I am not sure if I understood well, but when I do it then I have only two > columns left: > >> L3 <- LETTERS[1:3] >> (d <- data.frame(cbind(x=1, y=1:10, z=11:20), fac=sample(L3, 10, >> replace=TRUE))) > x y z fac > 1 1 1 11 C > 2 1 2 12 B > 3 1 3 13 B > 4 1 4 14 C > 5 1 5 15 C > 6 1 6 16 B > 7 1 7 17 C > 8 1 8 18 C > 9 1 9 19 B > 10 1 10 20 C >> d <- d[,c(2,1)] >> d > y x > 1 1 1 > 2 2 1 > 3 3 1 > 4 4 1 > 5 5 1 > 6 6 1 > 7 7 1 > 8 8 1 > 9 9 1 > 10 10 1 > > But I have more than two columns in my data.frame.In your case, it should be # Swap the two first columns d <- d[,c(2,1,3,4)] # Swap column 2 and 3 d <- d[,c(1,3,2,4)] Notice that my data frame had only two columns. Paul
That works perfect. Thanks a lot Paul! Greets Birgit Paul Smith wrote:> > On Mon, Jun 2, 2008 at 1:04 PM, Birgitle <birgit.lemcke at systbot.uzh.ch> > wrote: >> >> Thanks Paul. >> >> I am not sure if I understood well, but when I do it then I have only two >> columns left: >> >>> L3 <- LETTERS[1:3] >>> (d <- data.frame(cbind(x=1, y=1:10, z=11:20), fac=sample(L3, 10, >>> replace=TRUE))) >> x y z fac >> 1 1 1 11 C >> 2 1 2 12 B >> 3 1 3 13 B >> 4 1 4 14 C >> 5 1 5 15 C >> 6 1 6 16 B >> 7 1 7 17 C >> 8 1 8 18 C >> 9 1 9 19 B >> 10 1 10 20 C >>> d <- d[,c(2,1)] >>> d >> y x >> 1 1 1 >> 2 2 1 >> 3 3 1 >> 4 4 1 >> 5 5 1 >> 6 6 1 >> 7 7 1 >> 8 8 1 >> 9 9 1 >> 10 10 1 >> >> But I have more than two columns in my data.frame. > > In your case, it should be > > # Swap the two first columns > d <- d[,c(2,1,3,4)] > # Swap column 2 and 3 > d <- d[,c(1,3,2,4)] > > Notice that my data frame had only two columns. > > Paul > > ______________________________________________ > 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. > >Bb205Lc ----- The art of living is more like wrestling than dancing. (Marcus Aurelius) -- View this message in context: http://www.nabble.com/Swap-variables-in-data.frame-tp17597476p17600374.html Sent from the R help mailing list archive at Nabble.com.
Birgit Lemcke wrote:> I have a dataframe and two of my variables are in the wrong position > and I would like to swap those variables.In addition to the other solutions posted, if you prefer to reference the columns by name rather than by index, you could use subset() dat <- data.frame(a = letters[1:3], b = LETTERS[1:3], c = 1:3, d = 3:1) subset(dat, select = c(b, a, c, d)) ## or equivalently, something like: subset(dat, select = c(b:a, c:d)) --Jim James A. Rogers, Ph.D. Associate Director, Neuroscience Statistics Pfizer Global R&D New London 50 Pequot Avenue (MS 6025-B2131) New London, CT 06320 office: (860) 732-0783 cell: (860) 501-7228 fax: (860) 686-7866
Thanks might be easier in my case because I have so many variables. Could have found this solution on my own. Birgit Rogers, James A [PGRD Groton] wrote:> > > Birgit Lemcke wrote: > >> I have a dataframe and two of my variables are in the wrong position >> and I would like to swap those variables. > > In addition to the other solutions posted, if you prefer to reference > the columns by name rather than by index, you could use subset() > > dat <- data.frame(a = letters[1:3], b = LETTERS[1:3], c = 1:3, d = 3:1) > > subset(dat, select = c(b, a, c, d)) > ## or equivalently, something like: > subset(dat, select = c(b:a, c:d)) > > > --Jim > > James A. Rogers, Ph.D. > Associate Director, Neuroscience Statistics > Pfizer Global R&D New London > 50 Pequot Avenue (MS 6025-B2131) > New London, CT 06320 > office: (860) 732-0783 > cell: (860) 501-7228 > fax: (860) 686-7866 > > > > ______________________________________________ > 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. > >----- The art of living is more like wrestling than dancing. (Marcus Aurelius) -- View this message in context: http://www.nabble.com/Swap-variables-in-data.frame-tp17597476p17626237.html Sent from the R help mailing list archive at Nabble.com.