0

I want to create different kind of plots of some data frame objects. I have 25 data frame objects in my workspace but I only want the plots of 16 these.

Is there any why to create a for loop, selecting only these 16 objects?

I don't know if it helps, but the 16 objects' names starts with the word "top_", and all of them have the same number of rows and columns (all other objects have different size)

Regards,

user2794659
  • 145
  • 2
  • 14
  • 3
    Please provide a minimal (e.g. 4 tiny data frames, of which you want to plot 2), [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and show us what you have tried. – Henrik Sep 21 '13 at 14:17
  • Welcome to the site, @user2794659. You need to add a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for this question to be answerable. – gung - Reinstate Monica Sep 21 '13 at 14:18

2 Answers2

1

This may help:

 ls(pattern="^top_")
Ferdinand.kraft
  • 12,579
  • 10
  • 47
  • 69
1

Note that if you have multiple data frames (or other objects) that you want to do the exact same thing with then in the long run your life will be easier if you put these data frames/objects into a single list and work with them there.

One way to quickly put your data frames into a list is with mget:

mydata <- mget( ls(pat='^top_') )

Then you can remove the copies in the global environment with:

rm( list=names(mydata) )

Now if you want to plot the x and y column from each data frame you could do:

lapply( mydata, function(df) plot(df$x, df$y) )

or

lapply( names(mydata), function(dfn) plot( y ~ x, data=mydata[[dfn]], main=dfn ) )

or

for( i in seq_along(mydata) ) {
  plot( y ~ x, data= mydata[[i]], main=names(mydata)[[i]] )
}

or

...

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • Thank you Greg, for this useful info! – user2794659 Sep 23 '13 at 00:21
  • Hi guys, something that you may already know... but maybe it's useful for someone: this code lapply(ls(pattern="^top_"), function(x) get(x)) seems equal to this mget( ls(pat='^top_') ) in the sense that both create a list with elements. Although, with the first code you don't obtain the names! which could be a big drawback! – user2794659 Sep 24 '13 at 08:38