[R] how to process multiple data files using R loop

William Dunlap wdunlap at tibco.com
Sat Aug 9 01:58:23 CEST 2014


>  for(i in ls(pattern="P_")){ head(get(i), 2)}  # Should work.

You also need to use print(head(...)) if you want to see the printed
output from each iteration.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Fri, Aug 8, 2014 at 4:36 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Aug 8, 2014, at 11:25 AM, Fix Ace wrote:
>
>> I have 16 files and would like to check the information of their first two lines, what I did:
>>
>>
>>> ls(pattern="P_")
>>  [1] "P_3_utr_source_data"               "P_5_utr_source_data"
>>  [3] "P_exon_per_gene_cds_source_data"   "P_exon_per_gene_source_data"
>>  [5] "P_exon_source_data"                "P_first_exon_oncds_source_data"
>>  [7] "P_first_intron_oncds_source_data"  "P_first_intron_ongene_source_data"
>>  [9] "P_firt_exon_ongene_source_data"    "P_gene_cds_source_data"
>> [11] "P_gene_source_data"                "P_intron_source_data"
>> [13] "P_last_exon_oncds_source_data"     "P_last_exon_ongene_source_data"
>> [15] "P_last_intron_oncds_source_data"   "P_last_intron_ongene_source_data"
>>
>>
>
> The results from `ls()` are not actual R names but rather are character vectors. To "promote" a character value to an R language-name you need the `get` function:
>
>>
>>> for(i in ls(pattern="P_")){head(i, 2)}
>>
>
>  for(i in ls(pattern="P_")){ head(get(i), 2)}  # Should work.
>
> David.
>
>
>> It obviously does not work since nothing came out
>>
>> What I would like to see for the output is :
>>
>>> head(P_3_utr_source_data,2)
>>   V1
>> 1  1
>> 2  1
>>> head(P_5_utr_source_data,2)
>>   V1
>> 1  1
>> 2  1
>>>
>> .
>>
>> .
>> .
>>
>>
>>
>> Could anybody help me with this?
>>
>> Thank you very much for your time:)
>>       [[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.
>
> David Winsemius
> Alameda, CA, USA
>
> ______________________________________________
> 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