0

I have many dataframes (139) each representing data from an animal. An exemple:

head(`88719.09`)
      date.sec      lon       lat lon.025   lat.025 lon.5   lat.5 lon.975   lat.975 bmode
1 -61860758400 -38.7792 -13.83299   -39.3 -14.56000 -38.8 -13.870   -38.1 -12.92975 1.467
2 -61860736800 -38.7865 -13.84709   -39.3 -14.49000 -38.8 -13.880   -38.2 -13.08000 1.682
3 -61860715200 -38.7511 -13.87840   -39.1 -14.40000 -38.7 -13.860   -38.4 -13.44000 1.735
4 -61860693600 -38.8559 -14.13090   -39.0 -14.27000 -38.9 -14.130   -38.8 -14.01000 1.675
5 -61860672000 -39.0026 -14.35851   -39.3 -14.68025 -39.0 -14.365   -38.7 -14.00000 1.875
6 -61860650400 -38.9631 -14.22969   -39.3 -14.62000 -38.9 -14.220   -38.6 -13.84975 1.870
  bmode.5
1       1
2       2
3       2
4       2
5       2
6       2

However, within each dataframe there is no column specifying which animal it is. In the example case, an ID column was required filled with "88719.09", like so:

teste$ID <- 88719.09
> head(teste)
      date.sec      lon       lat lon.025   lat.025 lon.5   lat.5 lon.975   lat.975 bmode
1 -61860758400 -38.7792 -13.83299   -39.3 -14.56000 -38.8 -13.870   -38.1 -12.92975 1.467
2 -61860736800 -38.7865 -13.84709   -39.3 -14.49000 -38.8 -13.880   -38.2 -13.08000 1.682
3 -61860715200 -38.7511 -13.87840   -39.1 -14.40000 -38.7 -13.860   -38.4 -13.44000 1.735
4 -61860693600 -38.8559 -14.13090   -39.0 -14.27000 -38.9 -14.130   -38.8 -14.01000 1.675
5 -61860672000 -39.0026 -14.35851   -39.3 -14.68025 -39.0 -14.365   -38.7 -14.00000 1.875
6 -61860650400 -38.9631 -14.22969   -39.3 -14.62000 -38.9 -14.220   -38.6 -13.84975 1.870
  bmode.5       ID
1       1 88719.09
2       2 88719.09
3       2 88719.09
4       2 88719.09
5       2 88719.09
6       2 88719.09

I joined all dataframes together, but I don't know what data is for which animal. I would like to create a column with the IDs for each object before joining them into a single dataframe. I would like to do this in an efficient way, instead of creating one column at a time for each of the 139 data frames.

Names of dataframes (objects)

ls()

102211.10,10946.05,111868.11,111868.16,111869.11,111869.17,111870.17,111871.12,112694.12,112696.17,112702.12,112712.12,112714.12,112717.12,112728.17,120937.17,120938.16,120942.17,120943.17,120947.12,120947.17,121189.12,121191.17,121192.12,121193.12,121195.12,121196.12,121203.17,121206.17,123226.17,171994.17,171997.17,172000.17,172001.17,172002.17,172003.17,172004.17,194591.19,194593.19,194601.19,194603.19,20162.03,20687.03,21791.03,21792.03,21800.03,21809.03,21810.03,24640.03,24641.05,24642.03,26712.05,27258.05,27259.03,27259.05,27259.06,27261.03,27261.05,27261.07,33000.05,33000.06,33001.05,33001.06,37229.05,37229.06,37230.06,37231.05,37231.07,37234.05,37234.06,37236.06,37282.06,37286.07,37288.06,37288.07,42521.06,42521.07,42525.07,50682.06,50682.07,50686.07,50687.07,60004.07,60007.07,7617.05,7618.05,81122.09,81123.09,81124.09,81125.09,81126.09,84480.12,84484.17,84485.17,84497.10,87624.10,87631.10,87632.12,87635.17,87759.08,87760.08,87761.08,87762.08,87763.08,87764.08,87765.08,87766.08,87767.08,87768.08,87768.11,87769.08,87769.11,87770.08,87771.09,87773.08,87773.09,87773.10,87773.11,87774.08,87774.09,87774.11,87775.08,87775.12,87776.08,87776.11,87776.17,87777.08,87777.10,87777.17,87778.08,87778.10,87780.17,87781.10,87783.09,87783.11,88719.09,88720.09,88724.10,88726.10,88727.09,96380.10

Can anybody help me? Thank you!

Todd Burus
  • 963
  • 1
  • 6
  • 20
Anne Elise
  • 133
  • 2
  • 9
  • Do you have the names of the dataframes stored in a list? – Todd Burus Jun 09 '20 at 12:55
  • I agree, would store your dataframes in a list to combine. You can create a new column with the source of each data frame easily. See examples [here](https://stackoverflow.com/questions/30150977/r-combine-list-of-data-frames-into-single-data-frame-add-column-with-list-inde) and other related strategies [here](https://stackoverflow.com/questions/2851327/convert-a-list-of-data-frames-into-one-data-frame). – Ben Jun 09 '20 at 13:02
  • You have a dataframe named `88719.09` ??? – Ronak Shah Jun 09 '20 at 13:12
  • If the data.frames are the only thing in the environment, you could iterate over the ouptput of ls(). If not you could iterate over ls(), testing if the item the object of that name is a data.frame – SmokeyShakers Jun 09 '20 at 13:15
  • @RonakShah The name of the object is 88719.09 – Anne Elise Jun 09 '20 at 13:18
  • @ToddBurus I edited my post and add the names of the dataframes (objects) – Anne Elise Jun 09 '20 at 13:22
  • Did you try my answer below? I think it should work. If you have only the names in `ls()` that you want to combine then you don't need any pattern to look for `dplyr::bind_rows(mget(ls()), .id = "ID")` should work. – Ronak Shah Jun 09 '20 at 23:42
  • Thanks @RonakShah! It worked very well! – Anne Elise Jun 10 '20 at 13:00

1 Answers1

1

You need to come up with a pattern that clearly identifies all your dataframes. The example you have shared shows that you have dataframe named "88719.09".

If other dataframes follow similar pattern you can use regex '\\d+\\.\\d+' which represents number followed by decimal place followed by another number. When we pass this pattern in ls command it will return objects in the environment which satisfy this pattern. You can use mget to get a named list of dataframes and then use bind_rows from dplyr or rbindlist from data.table both of which does the same thing.

dplyr::bind_rows(mget(ls(pattern = '\\d+\\.\\d+')), .id = "ID")

Or using data.table :

data.table::rbindlist(mget(ls(pattern = '\\d+\\.\\d+')), idcol = "ID")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213