Re: [R] Inserting column in between -- "better" way?

2011-08-02 Thread peter dalgaard
On Aug 2, 2011, at 19:17 , Bert Gunter wrote: > Thanks for this Peter: > >> >> Sarah (sic) is on the right track, just lose the commas so that you don't >> drop to a vector: >> >>> x <- data.frame(A=1:3, B=1:3, C=1:3, D=1:3, E=1:3) >>> newcol <- 4:6 >>> cbind(x[1], newcol, x[2:ncol(x)]) >> A

Re: [R] Inserting column in between -- "better" way?

2011-08-02 Thread Bert Gunter
Thanks for this Peter: > > Sarah (sic) is on the right track, just lose the commas so that you don't > drop to a vector: > >> x <- data.frame(A=1:3, B=1:3, C=1:3, D=1:3, E=1:3) >> newcol <- 4:6 >> cbind(x[1], newcol, x[2:ncol(x)]) >  A newcol B C D E > 1 1      4 1 1 1 1 > 2 2      5 2 2 2 2 > 3

Re: [R] Inserting column in between -- "better" way?

2011-08-02 Thread peter dalgaard
On Aug 1, 2011, at 20:50 , David L Carlson wrote: > Actually Sara's method fails if the insertion is after the first or before > the last column: > >> x <- data.frame(A=1:3, B=1:3, C=1:3, D=1:3, E=1:3) >> newcol <- 4:6 >> cbind(x[,1], newcol, x[,2:ncol(x)]) > Sarah (sic) is on the right track,

Re: [R] Inserting column in between -- "better" way?

2011-08-01 Thread David L Carlson
Anthropology Texas A&M University College Station, TX 77843-4352 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Sarah Goslee Sent: Monday, August 01, 2011 12:44 PM To: Bert Gunter Cc: r-help@r-project.org Subject: Re: [R]

Re: [R] Inserting column in between

2011-08-01 Thread Bert Gunter
Thanks Sarah and David. Yes, but note this: > z <- data.frame(a=1:2,b=3:4) > z a b 1 1 3 2 2 4 > newdat <- 5:6 > cbind(z[,1],newdat,z[,2]) newdat [1,] 1 5 3 [2,] 2 6 4 > cbind.data.frame(z[,1],newdat,z[,2]) z[, 1] newdat z[, 2] 1 1 53 2 2 6

Re: [R] Inserting column in between -- "better" way?

2011-08-01 Thread Ista Zahn
On Mon, Aug 1, 2011 at 1:43 PM, Sarah Goslee wrote: > Bert, > > On Mon, Aug 1, 2011 at 1:27 PM, Bert Gunter wrote: >> Folks: >> >> I consider my reply below rather clumsy: One has to keep track of >> index numbers other than that which is inserted and must separately >> change column names. Is th

Re: [R] Inserting column in between -- "better" way?

2011-08-01 Thread Sarah Goslee
Bert, On Mon, Aug 1, 2011 at 1:27 PM, Bert Gunter wrote: > Folks: > > I consider my reply below rather clumsy: One has to keep track of > index numbers other than that which is inserted and must separately > change column names. Is there as "essentially better" way to do this, > either via base R

Re: [R] Inserting column in between

2011-08-01 Thread Sarah Goslee
Bert, On Mon, Aug 1, 2011 at 1:17 PM, Bert Gunter wrote: > Doesn't work -- you lose column names. But I don't lose column names: > x <- data.frame(A=1:3, B=1:3, C=1:3, D=1:3, E=1:3) > x A B C D E 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 > newcol <- 4:6 > cbind(x[,1:2], newcol, x[,3:ncol(x)]) A B

Re: [R] Inserting column in between

2011-08-01 Thread David L Carlson
ropology Texas A&M University College Station, TX 77843-4352 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Bert Gunter Sent: Monday, August 01, 2011 12:18 PM To: Sarah Goslee Cc: r-help@r-project.org Subject: Re: [R] Inserting colu

Re: [R] Inserting column in between -- "better" way?

2011-08-01 Thread Bert Gunter
Folks: I consider my reply below rather clumsy: One has to keep track of index numbers other than that which is inserted and must separately change column names. Is there as "essentially better" way to do this, either via base R or via an R package. I leave it to you to define "essentially better.

Re: [R] Inserting column in between

2011-08-01 Thread Bert Gunter
Doesn't work -- you lose column names. Try this instead: yourframe[,30:51] <- cbind( newcolumn,yourframe[,30:50]) Adjust column names after via: names(yourframe) [30:51] <- c(newcolname,names(yourframe[30:50]) Cheers, Bert On Mon, Aug 1, 2011 at 10:10 AM, Sarah Goslee wrote: > x <- cbind(x[,

Re: [R] Inserting column in between

2011-08-01 Thread Sarah Goslee
x <- cbind(x[,1:29], newcolumn, x[,30:ncol(x)]) On Mon, Aug 1, 2011 at 12:59 PM, Bansal, Vikas wrote: > Dear all, > > I have a very simple question.I have data frame of 50 columns and i want to > insert a column in 30th position.But i do not want to delete that column.Is > it possible to includ

[R] Inserting column in between

2011-08-01 Thread Bansal, Vikas
Dear all, I have a very simple question.I have data frame of 50 columns and i want to insert a column in 30th position.But i do not want to delete that column.Is it possible to include a column in between, so that new values are in 30th column and 30 th column is now 31st and 31st is 32nd..