Search code examples
rdataframemetadataorganization

Organization of data with metadata


I have a dataframe that contains two columns X-data and Y-data. This represents some experimental data.

Now I have a lot of additional information that I want to associate with this data, such as temperatures, flow rates and so on the sample was recorded at. I have this metadata in a second dataframe.

The data and metadata should always stay together, but I also want to be able to do calculations with the data

As I have many of those data-metadata pairs (>100), I was wondering what people think is an efficient way to organize the data?

For now, I have the two dataframes in a list, but I find accessing the individual values or data-columns tedious (= a lot of code and brackets to write).


Solution

  • You can use an attribute:

    dfr <- data.frame(x=1:3,y=rnorm(3))
    meta <- list(temp="30C",date=as.Date("2013-02-27"))
    attr(dfr,"meta") <- meta
    
    dfr
      x          y
    1 1 -1.3580532
    2 2 -0.9873850
    3 3  0.3809447
    
    attr(dfr,"meta")
    $temp
    [1] "30C"
    
    $date
    [1] "2013-02-27"
    
    str(dfr)
    'data.frame':   3 obs. of  2 variables:
     $ x: int  1 2 3
     $ y: num  -1.358 -0.987 0.381
     - attr(*, "meta")=List of 2
      ..$ temp: chr "30C"
      ..$ date: Date, format: "2013-02-27"