Melting data.tables

Joining Data with data.table in R

Scott Ritchie

Postdoctoral Researcher in Systems Genomics

Melting a wide data.table

Joining Data with data.table in R

The `melt()` function

Use measure.vars to specify columns to stack:

melt(sales_wide, measure.vars = c("2015", "2016"))
   quarter variable   value
1:       1     2015 3200100
2:       2     2015 2950000
3:       3     2015 2980700
4:       4     2015 3420000
5:       1     2016 3350000
6:       2     2016 3000300
7:       3     2016 3120200
8:       4     2016 3670000
Joining Data with data.table in R

The `melt()` function

Use variable.name and value.name to rename these columns in the result:

melt(sales_wide, measure.vars = c("2015", "2016"), 
     variable.name = "year", value.name = "amount")
   quarter year  amount
1:       1 2015 3200100
2:       2 2015 2950000
3:       3 2015 2980700
4:       4 2015 3420000
5:       1 2016 3350000
6:       2 2016 3000300
7:       3 2016 3120200
8:       4 2016 3670000
Joining Data with data.table in R

The `melt()` function

Use id.vars to specify columns to keep aside

melt(sales_wide, id.vars = "quarter", 
     variable.name = "year", value.name = "amount")
   quarter year  amount
1:       1 2015 3200100
2:       2 2015 2950000
3:       3 2015 2980700
4:       4 2015 3420000
5:       1 2016 3350000
6:       2 2016 3000300
7:       3 2016 3120200
8:       4 2016 3670000
Joining Data with data.table in R

The `melt()` function

Use both to keep only a subset of columns

melt(sales_wide, id.vars = "quarter", measure.vars = "2015", 
     variable.name = "year", value.name = "amount")
   quarter year  amount
1:       1 2015 3200100
2:       2 2015 2950000
3:       3 2015 2980700
4:       4 2015 3420000
Joining Data with data.table in R

Let's practice!

Joining Data with data.table in R

Preparing Video For Download...