Re: [R] inserting columns in the middle of a dataframe

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri 29 Sep 2006 - 06:18:00 GMT

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@stat.math.ethz.ch
>>> [mailto:r-help-bounces@stat.math.ethz.ch] 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,                  ripley@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

______________________________________________
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.
Received on Fri Sep 29 16:22:45 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 29 Sep 2006 - 07:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.