R Container for Multiple data.frame with a Brief Description of the Content of the data.frame For a project I have a large dataset with a multitude of variables from different questionnaires. Not all variables are required for all analyses.
So I created a preprocessing script, in which subsets of variables (with and without abbreviations) are created. However it gets confusing pretty fast.
For convenience I decided to create a index_list which holds all data.frames as well as a data.frame called index_df which holds the name of the respective data.frame as well as a brief description of each subversion of the dataset.
######################## Preparation / Loading #####################
# Clean Out Global Environment
rm(list=ls())

# Detach all unnecessary pacakges
pacman::p_unload()

# Load Required Libraries
pacman::p_load(dplyr, tidyr, gridExtra, conflicted)

# Load Data
#source(&quot;00_Preprocess.R&quot;)
#create simulation data instead
sub_data &lt;- data.frame(x=c(2,3,5,1,6),y=c(20,30,10,302,5))
uv_newScale &lt;- data.frame(item1=c(2,3,5,1,6),item2=c(3,5,1,3,2))

# Resolving conflicted Namepsaces
conflict_prefer(&quot;filter&quot;, &quot;dplyr&quot;)

# Creating an Index 
index_list &lt;- list(&quot;sub_data&quot;=sub_data,
                   &quot;uv_newScale&quot;=uv_newScale
                   )

index_df &lt;- data.frame(&quot;Data.Frame&quot;=c(&quot;sub_data&quot;,
                                        &quot;uv_newScale&quot;),
                       &quot;Description&quot;=c(&quot;Contains all sumscales + sociodemographics, names abbreviated&quot;,
                                       &quot;Only sum scores for the UV Scale&quot;))

I am wondering if there is a more efficient way to do so. Like saving the data.frames together with the description in one container?
One approach
If the description is to serve only as a metadata, R allows you to add any number of metadata to objects with attributes feature. you can add it as an attribute for the data.frame object.
attr(sub_data, &quot;Description&quot;) &lt;- &quot;Contains all sumscales + sociodemographics, names abbreviated&quot;

# Description now shows in the data frame structure 
str(sub_data)
# 'data.frame': 5 obs. of  2 variables:
#  $ x: num  2 3 5 1 6
# $ y: num  20 30 10 302 5
# - attr(*, &quot;Description&quot;)= chr &quot;Contains all sumscales + sociodemographics, names # abbreviated&quot;

#You can access the Description only
attributes(sub_data)$Description
# [1] &quot;Contains all sumscales + sociodemographics, names abbreviated&quot;

However, custom attributes come with limitations.They are not persistent when you perform certain operations on objects such as subsetting. Here is an example using same object with the new Description attribute we just added. If we subset the data, the custom attribute will be lost.
sub2_data &lt;- sub_data[,&quot;x&quot;, drop = FALSE]

attributes(sub2_data)$Description
# NULL


Alternative approach
You can use the same idea of creating a container within container. However, instead of creating a list that contains data frames, you can create a data.frame within a data.frame. This makes it easier to access and manipulate. You can access the inner data frame by adding second $
# Assigning new column `data` to hold data frames 
index_df$data &lt;- index_list

# We can access Description 
index_df$Description
# [1] &quot;Contains all sumscales + sociodemographics, names abbreviated&quot;
# [2] &quot;Only sum scores for the UV Scale&quot;  

# Accessing Data 
index_df$data$sub_data
#  x   y
#1 2  20
#2 3  30
#3 5  10
#4 1 302
#5 6   5

str(index_df)
# 'data.frame': 2 obs. of  3 variables:
#  $ Data.Frame : chr  &quot;sub_data&quot; &quot;uv_newScale&quot;
# $ Description: chr  &quot;Contains all sumscales + sociodemographics, names abbreviated&quot; &quot;Only sum scores for the UV Scale&quot;
# $ data       :List of 2
#  ..$ sub_data   :'data.frame':    5 obs. of  2 variables:
#  .. ..$ x: num  2 3 5 1 6
#  .. ..$ y: num  20 30 10 302 5
#  ..$ uv_newScale:'data.frame':    5 obs. of  2 variables:
#  .. ..$ item1: num  2 3 5 1 6
#  .. ..$ item2: num  3 5 1 3 2

Efficiency
The first approach is more efficient than the second in terms of memory footprint. Here is a comparison between objects sizes in bytes.
# Creating one data frame within one data frame for comparison
list_df &lt;- list(sub_data=sub_data)
dfs &lt;- data.frame(&quot;Data.Frame&quot;=&quot;sub_data&quot;,
             &quot;Description&quot;=&quot;Contains all sumscales + sociodemographics, names abbreviated&quot;)

dfs$data &lt;- list_df 

# Adding attributes
attr(sub_data, &quot;Description&quot;) &lt;- &quot;Contains all sumscales + sociodemographics, names abbreviated&quot;
 

# Memory Size in bytes
object.size(dfs)
# 2376 bytes
object.size(sub_data)
# 1224 bytes