[R] Looping through R objects
Rolf Turner
r.turner at auckland.ac.nz
Wed Jul 29 00:23:44 CEST 2009
On 29/07/2009, at 9:39 AM, Vivek Ayer wrote:
> That just creates one object and doesn't contain any $ subobjects in
> it. Here's what I did to figure it out (It's complicated):
>
> for(i in c(1:13))
> assign(paste("bc",i,sep=""),read.csv(paste
> (i,".csv",sep=""),sep="",header=TRUE))
>
> in shorthand: for {assign(paste,read(paste))}
>
> This creates individual objects for each csv file and allows me to
> have $ subjects, e.g., bc1$foo.
I don't know what you mean by ``doesn't contain any $ subobjects'',
but Steve's suggested approach is much better than yours in terms
of convenient processing of the results. It's much better, generally
speaking, to have related objects bundled together into a list, which
is what Steve's approach does, rather than to have a large number of
individual objects floating around in your work space, which is what
your method does.
I would suggest that you learn about lists and the techniques for
addressing and extracting their components. It will pay off in saving
you a great deal of time and effort in the future.
cheers,
Rolf Turner
>
> Thanks again,
> Vivek
>
> On Tue, Jul 28, 2009 at 2:12 PM, Steve
> Lianoglou<mailinglist.honeypot at gmail.com> wrote:
>> Hi,
>>
>> On Jul 28, 2009, at 4:54 PM, Vivek Ayer wrote:
>>
>>> Hi all,
>>>
>>> I have 13 csv files and I want to assign each csv file to one
>>> object,
>>> i.e.,
>>>
>>> bc1 <- read.csv("1.csv,header=TRUE,sep="")
>>> bc2 <- read.csv("2.csv,header=TRUE,sep="")
>>> bc3 ...
>>>
>>> So I want to create 13 objects. How could I automate this with a
>>> for loop?
>>>
>>> for (i in c(1:13)) ...
>>>
>>> Any ideas?
>>
>>
>> objects <- lapply(1:13, function(i) {
>> read.csv(paste(i, 'csv', sep='.'), header=TRUE, sep="")
>> })
>>
>> -steve
>>
>> --
>> Steve Lianoglou
>> Graduate Student: Computational Systems Biology
>> | Memorial Sloan-Kettering Cancer Center
>> | Weill Medical College of Cornell University
>> Contact Info: http://cbio.mskcc.org/~lianos/contact
>>
>>
>
> ______________________________________________
> 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.
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
More information about the R-help
mailing list