[R] help to add a new column filled with value 1
    Marianne Promberger 
    marianne.promberger at kcl.ac.uk
       
    Tue Nov  9 16:48:23 CET 2010
    
    
  
Mohan L <l.mohanphysics at gmail.com> 09-Nov-10 14:25:
> Dear All,
> 
> I have a data frame with  5 column and 201 row data. I want to add one
> more column between column 1 and 2 with value of 1. So the new column
> has to be the second column filled with 1. Any help will be
> appreciated.
You need two steps
Assume your data frame "main":
> main
   name id memory storage
1 mohan  1  100.2     1.1
2   ram  1  200.0   100.0
3 kumar  1  400.0    50.0
4   xxx  1  100.0    40.0
5   aaa  1  800.0    45.0
6 mount  1  200.0    80.0
main$newcol <- rep(1,nrow(main)) # make new column
main
   name id memory storage newcol
1 mohan  1  100.2     1.1      1
2   ram  1  200.0   100.0      1
3 kumar  1  400.0    50.0      1
4   xxx  1  100.0    40.0      1
5   aaa  1  800.0    45.0      1
6 mount  1  200.0    80.0      1
main[,c(1,5,2,3,4)] # order columns by indexing
-- 
Marianne Promberger PhD, King's College London
http://promberger.info
R version 2.12.0 (2010-10-15)
Ubuntu 9.04
    
    
More information about the R-help
mailing list