[R] inserting columns in the middle of a dataframe
Christos Hatzis
christos at nuverabio.com
Thu Sep 14 01:20:19 CEST 2006
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:jm540 at york.ac.uk]
Sent: Wednesday, September 13, 2006 6:43 PM
To: christos at nuverabio.com; r-help at 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: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