[R] inserting columns in the middle of a dataframe

Jon Minton jm540 at york.ac.uk
Thu Sep 14 00:42:43 CEST 2006


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.



More information about the R-help mailing list