[R] Converting Strings to Variable names
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]] __ 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.
Re: [R] Converting Strings to Variable names
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@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. __ 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.
Re: [R] Converting Strings to Variable names
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 amb1netwo...@gmail.comwrote: 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, and you 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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[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 and provide commented, minimal, self-contained, reproducible code.