[R] Converting Strings to Variable names

Erik Iverson eriki at ccbr.umn.edu
Thu Nov 4 16:11:25 CET 2010



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.



More information about the R-help mailing list