Skip to content
Prev 267393 / 398502 Next

Inserting column in between -- "better" way?

On Aug 1, 2011, at 20:50 , David L Carlson wrote:

            
Sarah (sic) is on the right track, just lose the commas so that you don't drop to a vector:
A newcol B C D E
1 1      4 1 1 1 1
2 2      5 2 2 2 2
3 3      6 3 3 3 3

Also notice that there is a named form of cbind
A foo B C D E
1 1   4 1 1 1 1
2 2   5 2 2 2 2
3 3   6 3 3 3 3


and that things will work (mostly) with matrices and data frames too:
A x y B C D E
1 1 4 6 1 1 1 1
2 2 5 5 2 2 2 2
3 3 6 4 3 3 3 3
A x y B C D E
1 1 4 6 1 1 1 1
2 2 5 5 2 2 2 2
3 3 6 4 3 3 3 3

(The "mostly" bit refers to some slight oddness occurring if you cbind a matrix with no column names:
A 1 2 B C D E
1 1 4 7 1 1 1 1
2 2 5 8 2 2 2 2
3 3 6 9 3 3 3 3

)