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

From: David Barron <mothsailor_at_googlemail.com>
Date: Wed 13 Sep 2006 - 22:20:05 GMT

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]]

______________________________________________
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 Thu Sep 14 08:23:20 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 Wed 13 Sep 2006 - 22:30:06 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.