Dear R users: Is there a built-in and simple way to insert new columns after other columns in a dataframe? I.e. currently I have: V1 V2 V3 V4 [1,] [2,] Etc. But I want V1 V5 V2 V3 V4 [1,] [2,] Etc. Can this be done in one line? Jon Minton [[alternative HTML version deleted]]
> Is there a built-in and simple way to insert new columns in a dataframe?You do this by collecting the columns in the new order you desire, and making a new frame. oldframe <- data.frame(matrix(0:14,ncol=3)) newcol <- data.frame(20:24) names(newcol) <- "newcol" newframe <- data.frame(c(oldframe[1],newcol, oldframe[2:3]))
Another way is to use attach(dataframe) x1 <- rep(1,10) x2 <- rep(2,10) x3 <- rep(3,10) x4 <- rep(4,10) x5 <- rep(5,10) dat <- data.frame(x1,x2,x3,x4) rm(x1,x2,x3,x4) attach(dat) dat2 <- data.frame(x1,x5,x2,x3,x4) detach(dat) On 13/09/06, Timothy Bates <timothy.c.bates@gmail.com > wrote:> > > > Is there a built-in and simple way to insert new columns in a dataframe? > > > You do this by collecting the columns in the new order you desire, and > making a new frame. > > oldframe <- data.frame(matrix(0:14,ncol=3)) > newcol <- data.frame(20:24) > names(newcol) <- "newcol" > newframe <- data.frame(c(oldframe[1],newcol, oldframe[2:3])) > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP -- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP [[alternative HTML version deleted]]
See ?append -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jon Minton Sent: Wednesday, September 13, 2006 5:14 PM To: r-help at stat.math.ethz.ch Cc: 'Jon Minton' Subject: Re: [R] inserting columns in the middle of a dataframe Dear R users: Is there a built-in and simple way to insert new columns after other columns in a dataframe? I.e. currently I have: V1 V2 V3 V4 [1,] [2,] Etc. But I want V1 V5 V2 V3 V4 [1,] [2,] Etc. Can this be done in one line? Jon Minton [[alternative HTML version deleted]] ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.
Please folks -- use indexing. myframe<-myframe[,c(1,5,2,3,4)] Which begs the question: why bother rearranging the columns anyway, since one can get them used, printed, etc. in any order you wish anytime you want just by specifying the indices in the order you want them. I suspect the question was motivated by too much Sas- or Excel -ism. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Timothy Bates > Sent: Wednesday, September 13, 2006 3:05 PM > To: Jon Minton; r-help at stat.math.ethz.ch > Subject: Re: [R] inserting columns in the middle of a dataframe > > > > Is there a built-in and simple way to insert new columns in > a dataframe? > > You do this by collecting the columns in the new order you desire, and > making a new frame. > > oldframe <- data.frame(matrix(0:14,ncol=3)) > newcol <- data.frame(20:24) > names(newcol) <- "newcol" > newframe <- data.frame(c(oldframe[1],newcol, oldframe[2:3])) > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >
Thanks, but isn't that only for elements in vectors? I think I've found the following method to work: e.g. for df <- data.frame(v1,v2,v3,v4) use: df <- data.frame(df[1:2],v5,df[-c(1:2)]) I *believe* this is the one-line solution I was looking for. Can anyone see why this wouldn't work? Jon -----Original Message----- From: Christos Hatzis [mailto:christos at nuverabio.com] Sent: 13 September 2006 23:22 To: 'Jon Minton'; r-help at stat.math.ethz.ch Subject: RE: [R] inserting columns in the middle of a dataframe See ?append -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jon Minton Sent: Wednesday, September 13, 2006 5:14 PM To: r-help at stat.math.ethz.ch Cc: 'Jon Minton' Subject: Re: [R] inserting columns in the middle of a dataframe Dear R users: Is there a built-in and simple way to insert new columns after other columns in a dataframe? I.e. currently I have: V1 V2 V3 V4 [1,] [2,] Etc. But I want V1 V5 V2 V3 V4 [1,] [2,] Etc. Can this be done in one line? Jon Minton [[alternative HTML version deleted]] ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.
Berton Gunter wrote:> Please folks -- use indexing. > > myframe<-myframe[,c(1,5,2,3,4)] > > Which begs the question: why bother rearranging the columns anyway, since > one can get them used, printed, etc. in any order you wish anytime you want > just by specifying the indices in the order you want them. I suspect the > question was motivated by too much Sas- or Excel -ism.Many of the time series classes expect a date in the first column of the matrix or data.frame when creating the date-time object. Retrieving data in a SQL query from a dB returns a character representation of the date that requires conversion to a date. Performing this conversion is easy but inserting this converted date column is not straight forward.> > -- Bert Gunter > Genentech Non-Clinical Statistics > South San Francisco, CA > > "The business of the statistician is to catalyze the scientific learning > process." - George E. P. Box > > > >> -----Original Message----- >> From: r-help-bounces at stat.math.ethz.ch >> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Timothy Bates >> Sent: Wednesday, September 13, 2006 3:05 PM >> To: Jon Minton; r-help at stat.math.ethz.ch >> Subject: Re: [R] inserting columns in the middle of a dataframe >> >> >>> Is there a built-in and simple way to insert new columns in >> a dataframe? >> >> You do this by collecting the columns in the new order you desire, and >> making a new frame. >> >> oldframe <- data.frame(matrix(0:14,ncol=3)) >> newcol <- data.frame(20:24) >> names(newcol) <- "newcol" >> newframe <- data.frame(c(oldframe[1],newcol, oldframe[2:3])) >> >> ______________________________________________ >> 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 >> and provide commented, minimal, self-contained, reproducible code. >> > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >
Prof Brian Ripley
2006-Sep-29 06:18 UTC
[R] inserting columns in the middle of a dataframe
On Thu, 28 Sep 2006, Joe W. Byers wrote:> Berton Gunter wrote: >> Please folks -- use indexing. >> >> myframe<-myframe[,c(1,5,2,3,4)] >> >> Which begs the question: why bother rearranging the columns anyway, since >> one can get them used, printed, etc. in any order you wish anytime you want >> just by specifying the indices in the order you want them. I suspect the >> question was motivated by too much Sas- or Excel -ism.> Many of the time series classes expect a date in the first column of the > matrix or data.frame when creating the date-time object. Retrieving > data in a SQL query from a dB returns a character representation of the > date that requires conversion to a date. Performing this conversion is > easy but inserting this converted date column is not straight forward.Well-written R <--> DBMS software does return a date or date-time, and if it is the first column retrieved by other software, you want to _replace_ the _first_ column, not really relevant to the topic of your subject line. (Doing that is basic data manipulation, covered in Chapter 2 of MASS4, for example.) The initial assertion is not (necessarily) true of "ts" or "irts" or "its" or "zoo", so quite a few time-series class generators expect a date or date-time to be specified separately.>>> -----Original Message----- >>> From: r-help-bounces at stat.math.ethz.ch >>> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Timothy Bates >>> Sent: Wednesday, September 13, 2006 3:05 PM >>> To: Jon Minton; r-help at stat.math.ethz.ch >>> Subject: Re: [R] inserting columns in the middle of a dataframe >>> >>> >>>> Is there a built-in and simple way to insert new columns in >>> a dataframe? >>> >>> You do this by collecting the columns in the new order you desire, and >>> making a new frame. >>> >>> oldframe <- data.frame(matrix(0:14,ncol=3)) >>> newcol <- data.frame(20:24) >>> names(newcol) <- "newcol" >>> newframe <- data.frame(c(oldframe[1],newcol, oldframe[2:3]))-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595