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. Thanks for your time. Thanks & Rg Mohan L
Marianne Promberger
2010-Nov-09 15:48 UTC
[R] help to add a new column filled with value 1
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":> mainname 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
Mohan - Suppose your data frame is named "df". Try this: data.frame(df[,1],1,df[,2:5]) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Tue, 9 Nov 2010, Mohan L wrote:> 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. > > Thanks for your time. > > > Thanks & Rg > Mohan L > > ______________________________________________ > R-help at r-project.org 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. >
Here are 2 possibilities: cbind( iris[,1, drop=FALSE], 1, iris[,2:5] ) cbind( iris, 1) [ ,c(1,6,2:5) ] -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Mohan L > Sent: Tuesday, November 09, 2010 7:26 AM > To: r-help at r-project.org > Subject: [R] help to add a new column filled with value 1 > > 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. > > Thanks for your time. > > > Thanks & Rg > Mohan L > > ______________________________________________ > R-help at r-project.org 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.
Henrique Dallazuanna
2010-Nov-09 17:41 UTC
[R] help to add a new column filled with value 1
Try this: as.data.frame(append(iris, 1, after = 2)) On Tue, Nov 9, 2010 at 12:25 PM, Mohan L <l.mohanphysics@gmail.com> wrote:> 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. > > Thanks for your time. > > > Thanks & Rg > Mohan L > > ______________________________________________ > R-help@r-project.org 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]