Hi All, I would like to perform the same set of commands on the two different sets. I would to avoid writing the same set of command twice so I was wondering if there is an smart way to do this. I was thinking to use a loop as follows Data<-read.table(file=?data.txt?, header=T, sep=?\t?) D1<-Data[,1:30] D2<-Data[,30:60] ID<-c(?D1?,?D2?) for (i in 1:length(ID)){ data<-ID[i] } I can not assign D1 or D2 as data<-ID[i]? is there a way to do this? Thanks -- View this message in context: http://n4.nabble.com/performing-the-same-commands-on-two-different-data-sets-tp1474452p1474452.html Sent from the R help mailing list archive at Nabble.com.
Kjetil Halvorsen
2010-Feb-09 14:28 UTC
[R] performing the same commands on two different data sets
Did you try data<-ID[[i]] instead of your data<-ID[i]? Kjetil On Tue, Feb 9, 2010 at 11:19 AM, kayj <kjaja27 at yahoo.com> wrote:> > Hi All, > > > I would like to perform the same set of commands on the two different sets. > I would to avoid writing the same set of command twice so I was wondering if > there is an smart way to do this. I was thinking to use a loop as follows > > Data<-read.table(file=?data.txt?, header=T, sep=?\t?) > D1<-Data[,1:30] > D2<-Data[,30:60] > > ID<-c(?D1?,?D2?) > for (i in 1:length(ID)){ > data<-ID[i] > > } > > I can not assign D1 or D2 as data<-ID[i]? is there a way to do this? > > Thanks > > > -- > View this message in context: http://n4.nabble.com/performing-the-same-commands-on-two-different-data-sets-tp1474452p1474452.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Sarah Goslee
2010-Feb-09 14:45 UTC
[R] performing the same commands on two different data sets
You skipped a step: the get() function allows you to use the name of an object to load the object itself. ID<-c(?D1?,?D2?) for (i in 1:length(ID)){ data<- get(ID[i]) do stuff with data } Alternatively you could write a function that takes the object as its argument. myFunction <- function(data) { do stuff with data } myFunction(D1) myFunction(D2) Naming your objects data and Data is a recipe for confusion, though. Sarah On Tue, Feb 9, 2010 at 9:19 AM, kayj <kjaja27 at yahoo.com> wrote:> > Hi All, > > > I would like to perform the same set of commands on the two different sets. > I would to avoid writing the same set of command twice so I was wondering if > there is an smart way to do this. I was thinking to use a loop as follows > > Data<-read.table(file=?data.txt?, header=T, sep=?\t?) > D1<-Data[,1:30] > D2<-Data[,30:60] > > ID<-c(?D1?,?D2?) > for (i in 1:length(ID)){ > data<-ID[i] > > } > > I can not assign D1 or D2 as data<-ID[i]? is there a way to do this? > > Thanks > > > -- > View this message in context: http://n4.nabble.com/performing-the-same-commands-on-two-different-data-sets-tp1474452p1474452.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Sarah Goslee http://www.stringpage.com http://www.astronomicum.com http://www.functionaldiversity.org
thank you all for your help, the get() function worked perfectly. -- View this message in context: http://n4.nabble.com/performing-the-same-commands-on-two-different-data-sets-tp1474452p1474515.html Sent from the R help mailing list archive at Nabble.com.