Skip to content

how to process multiple data files using R loop

6 messages · Fix Ace, David Winsemius, William Dunlap +2 more

#
On Aug 8, 2014, at 11:25 AM, Fix Ace wrote:

            
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(get(i), 2)}  # Should work.

David.
David Winsemius
Alameda, CA, USA
#
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 09/08/14 06:25, Fix Ace wrote:
<SNIP>

Point of order:  You do ***NOT*** have 16 files as it stands.  The 
output from ls() indicates that you have 16 ***objects*** (presumably 
data frames) in your "work space" or "global environment".  (If the data 
were originally in 16 separate files, these files have apparently 
already been read into R.)

If you are going to use R, learn to distinguish the relevant concepts 
and to use the appropriate terminology.  Otherwise you will confuse 
everyone, including yourself, and get things totally wrong.  It is 
really no more difficult to use correct terminology than it is to use 
incorrect terminology --- and the former has the advantage of not 
misleading all concerned.


Dave Winsemius has already told you how to solve your immediate problem, 
using get().

cheers,

Rolf Turner
2 days later
#
In addition to the solution and comments that you have already
received, here are a couple of additional comments:

This is a variant on FAQ 7.21, if you had found that FAQ then it would
have told you about the get function.

The most important part of the answer in FAQ 7.21 is the last part
where it says that it is better to use a list.  If all the objects of
interest are related and you want to do the same or similar things to
each one, then having them all stored in a single list can simplify
things for the future.  You can collect all the objects into a single
list using the mget command, e.g.:

P_objects <- mget( ls(pattern='P_'))

Now that they are in a list you can do the equivalent of your loop,
but simpler with the lapply function, e.g.:

lapply( P_objects, head, 2 )

And if you want to do other things with all these objects, such as
save them, plot them, do a regression analysis on them, delete them,
etc. then you can do that using lapply/sapply as well in a simpler way
than looping.
On Fri, Aug 8, 2014 at 12:25 PM, Fix Ace <acefix at rocketmail.com> wrote:

  
    
#
Thank you very much for all replies:) Here is my working code:

for(i in ls(pattern="P_")){print(head(get(i),2))}
On Monday, August 11, 2014 11:04 AM, Greg Snow <538280 at gmail.com> wrote:
In addition to the solution and comments that you have already
received, here are a couple of additional comments:

This is a variant on FAQ 7.21, if you had found that FAQ then it would
have told you about the get function.

The most important part of the answer in FAQ 7.21 is the last part
where it says that it is better to use a list.? If all the objects of
interest are related and you want to do the same or similar things to
each one, then having them all stored in a single list can simplify
things for the future.? You can collect all the objects into a single
list using the mget command, e.g.:

P_objects <- mget( ls(pattern='P_'))

Now that they are in a list you can do the equivalent of your loop,
but simpler with the lapply function, e.g.:

lapply( P_objects, head, 2 )

And if you want to do other things with all these objects, such as
save them, plot them, do a regression analysis on them, delete them,
etc. then you can do that using lapply/sapply as well in a simpler way
than looping.
On Fri, Aug 8, 2014 at 12:25 PM, Fix Ace <acefix at rocketmail.com> wrote: