tutorials

A collection of useful code chunks and workflows


Extracting attribute metadata using R

Each dataset in the Arctic Data Center has associated metadata that displays on the data package page (for example, here), which can also be downloaded as an *.xml file.

You can scroll through attribute (variable) metadata in the Attribute Information section of a Data Table or Other Entity, but sometimes it’s easier to have examine this information using tabular data.

Thankfully, this is easily done using the EML package in R!

Start by downloading the EML file to a local directory. Alternatively, use RCurl to download the file from my github directory and follow along. Use read_eml() to load it into your R environment.

library(RCurl)
library(EML)

eml_web <- RCurl::getURL("https://raw.githubusercontent.com/isteves/tutorials/master/data/LeConte_meteo_metadata.xml")
eml <- read_eml(eml_web) 
# if you've downloaded the *.xml file, replace eml_web with your file path 
#(i.e., "home/isteves/my_metadata.xml")

We usually only have attributes for data tables, which we can grab from our EML using eml_get.

attributes <- eml_get(eml, "attributeList")

The variable, attributes, now has all the attribute information in the package stored as a list. For this example, we have three attribute objects.

class(attributes)
## [1] "list"
length(attributes)
## [1] 3

Each list object contains another list of 2 objects: (1) attribute information in the form of a dataframe and (2) factors to store information about enumerated domains within the dataset. A column titled “Fruit”, for example, may include data (c("o", "g", "p", "o")) as factors (o = orange, g = grape, p = pineapple).

To extract a single dataframe and save it to a csv, simply run:

attribute1 <- attributes[[1]]$attributes
write.csv(attribute1, "attribute1.csv")

To save all attributes, a for loop makes it easy:

for(i in 1:length(attributes)){
  attribute1 <- attributes[[i]]$attributes
  file_name <- paste0("attribute", i, ".csv")
  write.csv(attribute1, file_name)
}

…and you’re done!