0

I have a global environment of over 30 dataframes that will get even more filled as I write more. I needed to create separate dataframes for observations with data in each variable (almost as if they were separate groups). Hence why it is so large.

The dataframes all have the same data and column names:

ID    VOne   VTwo  VThree   VFour
123   2       1      1      1
101   3       1      1      1
ID   VOne    VTwo  VThree   VFour
140  1        3     1        1
112  1        3     1        1
115  1        2     1        1

As you can see, the only thing that differs is the variable with data larger than 1 and the number of observations. Is there a way to condense the dataframes into a larger set I can choose from so I can remove the dataframes from the global environment so that they don't bog it up?

I tried:

df_master_list <- list(df1, df2)

But it isn't easy to discern which dataframe is which (in my actual data, I have over 30 all with specific names). Can I rename the items in the list easily in one or two steps without creating a vector of over 30 names and setNames?

I am trying to find a way to rename the items in a list (or create a named list) without creating a list of 30 or more names. That is to say, I section in a list that say name should say the dataframe's name (like df1 or df2) instead of the default 1, 2, and so on.

AcidCatfish
  • 192
  • 8
  • Do you have only those objects in the global env – akrun Jan 04 '21 at 20:15
  • 1
    The question is closed, but I think the last part (removing the objects from environment) is not discussed in the linked questions. I would first, get the object names `df_objects <- ls(pattern = "^df[0-9]", envir = globalenv())` then put them in a list `df_list <- mget(df_objects, envir = globalenv())` and then remove the old copies `rm(list = df_objects)`. – TimTeaFan Jan 04 '21 at 21:12
  • I would always be wary removing objects in an automatic way. Accidentally cycling through commands may have unwanted results. That's why I only added a manual limited scope version of removing objects below. – Andre Wildberg Jan 04 '21 at 21:18
  • @akrun I have only 3 other dataframes which are raw data. – AcidCatfish Jan 04 '21 at 22:12
  • @TimTeaFan I was just going to remove using ```rm```. I want the list to have all the dataframes in it, but also include the names of the dataframe. That's the issue I'm having. – AcidCatfish Jan 04 '21 at 22:15
  • 1
    @AcidCatfish: `mget` returns a named list, so this should be the output you want. – TimTeaFan Jan 04 '21 at 22:18
  • @AndreWildberg: In the example I use a vector with string names to do both: pack the objects in a list, and remove them from global environment, so everything that is removed must be in the list. Nothing should be lost. But of course an unwanted object could end up in the list. – TimTeaFan Jan 04 '21 at 22:21

1 Answers1

1

Maybe this does what you want. You just need the list of objects, i.e. know your data.frame names:

df.list <- c("df1","df2")

df.new <- do.call( rbind, lapply( df.list, get ) )
#   ID VOne VTwo VThree VFour
#1 123    2    1      1     1
#2 101    3    1      1     1
#3 140    1    3      1     1
#4 112    1    3      1     1
#5 115    1    2      1     1

You can then do e.g. rm(df1) to remove some objects.

Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29