0

I want to put all the dataframes I have in a list format, so that I can merge all of them easily without typing the name of each dataframe and simply mentioning the list.

# importing netcdf files

files_so2 =  list.files(  pattern =  "*.nc" )

# reading each netcdf as a dataframe


for (i  in  1:length( files_so2 ) )  assign( 
                                             files_so2[i], as.data.frame( 
                                                                          brick(  files_so2[i] ),  xy = T   ) )

# I want to put all the files in the list so that I can perform the following


data <- Reduce (  function(x,y)  {merge(  x = x,  y  = y , by = c("x" , "y") )} , list_data)

I have 72 dataframes imported in R , now would like to put all of them in a single list say list_data as mentioned in the code to perform merging.

Rish2019
  • 1
  • 2
  • 1
    If the objects are already creted in the global env, you can use `mget` to get the data.frame in a `list`. .e.g if the object identifiers are 'df1', 'df2', ... 'df72', then `mget(ls(pattern = "^df\\d+$"))` or `mget(paste0("df", 1:72))` get the values of objects in a `list` – akrun May 20 '19 at 20:12
  • How did you create all these data.frames in the first place? It probably would have been better to read them directly into a list from the start. Working with 72 different variables probably isn't going to be fun. – MrFlick May 20 '19 at 20:17
  • The names of the file are : MERRA2_400.tavgM_2d_aer_Nx.201101.SUB.nc ; with the series changing by year- month. I dont want to rename dataframe at this stage as before merging I will use for loop to rename the value column(in each dataframe) based on filename (to assign it a year-month) – Rish2019 May 20 '19 at 20:22
  • 1
    `data_list = lapply(files_so2, function(x) as.data.frame(brick(x), xy = T))` is equivalent to your for loop but creating a list instead. – Gregor Thomas May 20 '19 at 20:34
  • @Gregor Thank! it worked well. – Rish2019 May 20 '19 at 20:48

0 Answers0