-4

I have over 200 data.frames in my global environment. I would like to remove the first row from each data.frame, but I am not sure how.

Any help will be appreciated please let me know if further information is needed.

Dre
  • 713
  • 1
  • 8
  • 27
  • How did you wind up with such a mess? It would probably be better to keep those data.frames in a list rather than polluting your global environment like that. They would be much easier to work with that way. – MrFlick Apr 28 '16 at 20:27
  • See [How to make a list of data frames](http://stackoverflow.com/a/24376207/903061) to follow-up on MrFlick's advice. – Gregor Thomas Apr 28 '16 at 21:44

1 Answers1

3

This will list all the data frames in your environment, remove the first row from each, and organize them into a list of data frames. Generally, better practice to have them in a list so you can more easily apply functions across them and access them.

df <- lapply(ls(), function(x) get(x)[-1,])

Update: good idea to check if objects are in fact data frames and only work with those. First we create a logical vector listing dataframes, then combine them into a list and remove the first row of each.

dfs = sapply(ls(), is.data.frame) 
lapply(mget(names(dfs)[dfs]), "[", -1, , drop = FALSE)

thanks to comments for finding my error and providing more efficient solutions

ano
  • 704
  • 5
  • 15
  • 2
    you should probably add a check that the object is a dataframe http://stackoverflow.com/questions/35387419/how-to-rbind-all-the-data-frames-in-your-working-environment-in-r/35387595#35387595 – user20650 Apr 28 '16 at 19:25