Re: [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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Timothy Bates Sent: Wednesday, September 13, 2006 3:05 PM To: Jon Minton; r-help@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, [EMAIL PROTECTED] 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, UKFax: +44 1865 272595 __ 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.
Re: [R] inserting columns in the middle of a dataframe
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Timothy Bates Sent: Wednesday, September 13, 2006 3:05 PM To: Jon Minton; r-help@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@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@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@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.
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@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.
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@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.
Re: [R] inserting columns in the middle of a dataframe
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 [EMAIL PROTECTED] 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]] __ 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.
Re: [R] inserting columns in the middle of a dataframe
See ?append -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jon Minton Sent: Wednesday, September 13, 2006 5:14 PM To: r-help@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@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@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.
Re: [R] inserting columns in the middle of a dataframe
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:[EMAIL PROTECTED] Sent: 13 September 2006 23:22 To: 'Jon Minton'; r-help@stat.math.ethz.ch Subject: RE: [R] inserting columns in the middle of a dataframe See ?append -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jon Minton Sent: Wednesday, September 13, 2006 5:14 PM To: r-help@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@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@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.
Re: [R] inserting columns in the middle of a dataframe
Sorry, I guess I did not explain at all how append could work in a one-liner: data.frame(df, v5)[append(1:4,5,2)] Your method is fine as well. The above might be more flexible if you need a more general solution, e.g. if you wanted to make it a function. -Christos -Original Message- From: Jon Minton [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 13, 2006 6:43 PM To: [EMAIL PROTECTED]; r-help@stat.math.ethz.ch Cc: 'Jon Minton' Subject: RE: [R] inserting columns in the middle of a dataframe 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:[EMAIL PROTECTED] Sent: 13 September 2006 23:22 To: 'Jon Minton'; r-help@stat.math.ethz.ch Subject: RE: [R] inserting columns in the middle of a dataframe See ?append -Christos -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jon Minton Sent: Wednesday, September 13, 2006 5:14 PM To: r-help@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@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@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.
Re: [R] inserting columns in the middle of a dataframe
I think it should be possible to create the column at the end and then use order on the columns names and indexes to only change the order of column indexes, rather than having to do operations on the data itself (which will be very time consuming if the dataset is large). Perhaps people with better R skills can suggest how to code this. Anupam. __ 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.