Creating data frame using structure() function in R

Structure() function is a simple, yet powerful function that describes a given object with given attributes. It is part of base R language library, so there is no need to load any additional library. And also, since the function was part of S-Language, it is in the base library from the earlier versions, making it backward or forward compatible.

Example:

dd <- structure(list( 
         year = c(2001, 2002, 2004, 2006) 
        ,length_days = c(366.3240, 365.4124, 366.5323423, 364.9573234)) 
        ,.Names = c("year", "length of days") 
        ,row.names = c(NA, -4L) 
        ,class = "data.frame")

All objects created using structure() – whether homogeneous (matrix, vector) or heterogeneous (data.frame, list) – have additional metadata information stored, using attributes. Like creating a simple vector with additional metadata information:

just_vector <- structure(1:10, comment = "This is my simple 
                                       vector with info")

And by using function:

attributes(just_vector)

We get the information back:

$`comment`
[1] "This is my simple vector with info"

In one go

So, let us suppose you want to create a structure (S3) in one step. The following would create a data.frame (heterogeneous) with several steps:

year = c(1999, 2002, 2005, 2008)
pollution = c(346.82,134.308821199349, 130.430379885892, 88.275457392443)
dd2 <- data.frame(year,pollution)
dd2$year <- as.factor(dd2$year)

Using structure, we can do this simpler and faster:

dd <- structure(list( 
   year = as.factor(c(2001, 2002, 2004, 2006))
  ,length_days = c(366.3240, 365.4124, 366.5323423, 364.9573234)) 
  ,.Names = c("year", "length of days") 
  ,row.names = c(NA, -4L) 
  ,class = "data.frame")

 

Useful cases when using structure() function are:

  • when creating a smaller data-set within your Jupyter  notebook (using Markdown )
  • when creating data-sets within your R code demo/example (and not using external CSV / TXT / JSON files)
  • when describing a given object with mixed data types (e.i.: data frame) and prepare it for data import
  • when creating many R environments and each have independent data-set
  • for persisting data
  • and many more…

Constructing data-frame with additional attributes and comments.

dd3 <- structure(list(
   v1 = as.factor(c(2001, 2002, 2004, 2006))
  ,v2 = I(c(2001, 2002, 2004, 2006))
  ,v3 = ordered(c(2001, 2002, 2004, 2006))
  ,v4 = as.double(c(366.3240, 365.4124, 366.5323423, 364.9573234)))
  ,.Names = c("year", "AsIs Year","yearO", "length of days")
  ,.typeOf = c("factor", "numeric", "ordered","numeric")
  ,row.names = c(NA, -4L)
  ,class = "data.frame"
  ,comment = "Ordered YearO for categorical analysis and other variables")

Nesting lists within lists can also be done, or even preserving the original data-sets as sub-list, hidden from the dataframe, can also be an option.

And checking comments can be done as:

attributes(dd3)$comment

attr(dd3, which="comment")

 

Both yield same results, as:

> attributes(dd3)$comment
[1] "Ordered YearO for categorical analysis and other variables"
> attr(dd3, which="comment")
[1] "Ordered YearO for categorical analysis and other variables"

 

This simple, yet very useful code example with effective function is as always, available at Github.

Happy Rrrring! 🙂

Tagged with: , , , ,
Posted in Uncategorized
3 comments on “Creating data frame using structure() function in R
  1. […] Tomaz Kastrun takes us through the structure() function in R: […]

    Like

  2. thelatemail says:

    I’m not sure I understand the advantage here. Wouldn’t you just use the `data.frame()` function like:

    data.frame(year=factor(c(1999, 2002, 2005, 2008)),
    pollution=c(346.82,134.308821199349, 130.430379885892, 88.275457392443))

    …and avoid the need to have to specify row.names and the class manually?

    Like

  3. […] Creating data frame using structure() function in R […]

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...