Hi all, I am processing 24 samples data and combine them in single table called CombinedSamples using following: CombinedSamples<-rbind(Sample1,Sample2,Sample3) Now variables Sample1, Sample2 and Sample3 have many different columns. To make it more flexible for other samples I'm replacing above code with a for loop: #Sample is a string vector containing all 24 sample names for (k in 1:length(Sample)) { CombinedSamples<-rbind(get(Sample[k])) } This code only stores last sample data as CombinedSample gets overwritten every time. Using "CombinedSamples[k]" or "CombinedSamples[k,]" causes dimension related errors as each Sample has several rows and not just 24. So how can I assign data of all 24 samples to CombinedSamples? Thanks, Anand [[alternative HTML version deleted]]
Anand Bambhania wrote:> Hi all, > > I am processing 24 samples data and combine them in single table called > CombinedSamples using following: > > CombinedSamples<-rbind(Sample1,Sample2,Sample3)Please use reproducible examples.> > Now variables Sample1, Sample2 and Sample3 have many different columns.Then you can't 'rbind' them, correct? From ?rbind: If there are several matrix arguments, they must all have the same number of columns (or rows) and this will be the number of columns (or rows) of the result.> > To make it more flexible for other samples I'm replacing above code with a > for loop: > > #Sample is a string vector containing all 24 sample names > > for (k in 1:length(Sample)) > { > CombinedSamples<-rbind(get(Sample[k])) > } > > This code only stores last sample data as CombinedSample gets overwritten > every time. Using "CombinedSamples[k]" or "CombinedSamples[k,]" causes > dimension related errors as each Sample has several rows and not just 24. So > how can I assign data of all 24 samples to CombinedSamples?I don't know since I'm unsure of the structure of these objects. If they all have the same structure, I'd store them in a list and do: CombinedSamples <- do.call(rbind, sampleList) otherwise perhaps using ?Reduce and ?merge. If you can provide a more complete example to the list, please do. You need not resort to a for loop/get hack for this. Best, --Erik> > Thanks, > > Anand > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Hi Anand, Try creating a variable where you can store your data, and append it in your loop. See added lines of code to include below... On Thu, Nov 4, 2010 at 9:43 AM, Anand Bambhania <amb1networks@gmail.com>wrote:> Hi all, > > I am processing 24 samples data and combine them in single table called > CombinedSamples using following: > > CombinedSamples<-rbind(Sample1,Sample2,Sample3) > > Now variables Sample1, Sample2 and Sample3 have many different columns. > > To make it more flexible for other samples I'm replacing above code with a > for loop: > > #Sample is a string vector containing all 24 sample names >#create a variable to stick your results res<- NULL> > for (k in 1:length(Sample)) > { > CombinedSamples<-rbind(get(Sample[k])) >res<-c(res, CombinedSamples)> } > > Now, every iteration of your loop should append CombinedSamples to res, andyou won't overwrite your results every time. HTH, Mike> This code only stores last sample data as CombinedSample gets overwritten > every time. Using "CombinedSamples[k]" or "CombinedSamples[k,]" causes > dimension related errors as each Sample has several rows and not just 24. > So > how can I assign data of all 24 samples to CombinedSamples? > > Thanks, > > Anand > > [[alternative HTML version deleted]] > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Seemingly Similar Threads
- computing marginal values based on multiple columns?
- conditional filter resulting in 2 new dataframes
- phantom NA/NaN/Inf in foreign function call (or something altogether different?)
- average columns of data frame corresponding to replicates
- Problem in converting natural numbers to bits and others