Comparisons with other packages

2024-02-21

Comparisons with other packages

This vignette provides comparisons with other packages that provide similar functionality. This is a work in progress – for a more detailed / complete / coherent comparison with other packages which provide wide-to-tall data reshaping, see my paper.

Are sepals larger or smaller than petals in the iris data?

Sometimes you want to melt a “wide” data table which has several distinct pieces of information encoded in each column name. One example is the familiar iris data, which have flower part and measurement dimension encoded in each of four column names:


library(data.table)
data.table(iris)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <fctr>
#>   1:          5.1         3.5          1.4         0.2    setosa
#>   2:          4.9         3.0          1.4         0.2    setosa
#>   3:          4.7         3.2          1.3         0.2    setosa
#>   4:          4.6         3.1          1.5         0.2    setosa
#>   5:          5.0         3.6          1.4         0.2    setosa
#>  ---                                                            
#> 146:          6.7         3.0          5.2         2.3 virginica
#> 147:          6.3         2.5          5.0         1.9 virginica
#> 148:          6.5         3.0          5.2         2.0 virginica
#> 149:          6.2         3.4          5.4         2.3 virginica
#> 150:          5.9         3.0          5.1         1.8 virginica

The goal in this section will be to convert these data into a format with a column for each flower part (Sepal and Petal) so we can easily make a facetted scatterplot to visually examine whether or not sepals or larger than petals. The easiest way to perform this conversion is with packages which provide a function for melting into multiple output columns:


iris.parts <- list(
  nc=nc::capture_melt_multiple(
    iris,
    column=".*?",
    "[.]",
    dim=".*"),
  tidyr=if(requireNamespace("tidyr"))tidyr::pivot_longer(
    iris, 
    cols=1:4, 
    names_to=c(".value", "dim"),
    names_sep="[.]"),
  stats=stats::reshape(
    iris,
    direction="long",
    timevar="dim",
    varying=1:4,
    sep="."),
  "data.table::melt"=melt(
    data.table(iris),
    measure.vars=patterns(
      Sepal="^Sepal",
      Petal="^Petal")
  )[data.table(
    variable=factor(1:2), dim=c("Length", "Width")
  ), on=.(variable)],
  if(requireNamespace("cdata"))cdata::rowrecs_to_blocks(
    iris,
    controlTable=data.frame(
      dim=c("Length", "Width"),
      Petal=c("Petal.Length", "Petal.Width"),
      Sepal=c("Sepal.Length", "Sepal.Width"),
      stringsAsFactors=FALSE),
    columnsToCopy="Species"))
#> Loading required namespace: cdata
iris.parts$nc
#>        Species    dim Petal Sepal
#>         <fctr> <char> <num> <num>
#>   1:    setosa Length   1.4   5.1
#>   2:    setosa Length   1.4   4.9
#>   3:    setosa Length   1.3   4.7
#>   4:    setosa Length   1.5   4.6
#>   5:    setosa Length   1.4   5.0
#>  ---                             
#> 296: virginica  Width   2.3   3.0
#> 297: virginica  Width   1.9   2.5
#> 298: virginica  Width   2.0   3.0
#> 299: virginica  Width   2.3   3.4
#> 300: virginica  Width   1.8   3.0

It is clear from the code above that each package is capable of the conversions. However the syntax and level of explicitness varies:

Any of the results can be visualized via:


if(require(ggplot2)){
  ggplot()+
    theme_bw()+
    theme(panel.spacing=grid::unit(0, "lines"))+
    facet_grid(dim ~ Species)+
    coord_equal()+
    geom_abline(slope=1, intercept=0, color="grey")+
    geom_point(aes(
      Petal, Sepal),
      data=iris.parts$nc)
}

It is clear from the plot above that sepals are larger than petals, for every measured flower.

Comparing dimensions in iris data

What if we wanted to compare dimensions rather than parts?


iris.dims <- list(
  nc=nc::capture_melt_multiple(
    iris,
    part=".*?",
    "[.]",
    column=".*"),
  stats=stats::reshape(
    structure(iris, names=sub("(.*?)[.](.*)", "\\2.\\1", names(iris))),
    direction="long",
    timevar="part",
    varying=1:4,
    sep="."))
iris.dims$nc
#>        Species   part Length Width
#>         <fctr> <char>  <num> <num>
#>   1:    setosa  Petal    1.4   0.2
#>   2:    setosa  Petal    1.4   0.2
#>   3:    setosa  Petal    1.3   0.2
#>   4:    setosa  Petal    1.5   0.2
#>   5:    setosa  Petal    1.4   0.2
#>  ---                              
#> 296: virginica  Sepal    6.7   3.0
#> 297: virginica  Sepal    6.3   2.5
#> 298: virginica  Sepal    6.5   3.0
#> 299: virginica  Sepal    6.2   3.4
#> 300: virginica  Sepal    5.9   3.0

The code above shows that the syntax is mostly the same for this example. The biggest difference is for stats::reshape which assumes that each input column name is composed of (1) the output column name, (2) a delimiter, and (3) some additional information to be stored in the output column given by timevar. Therefore we need to pre-process column names using sub for it to work.


if(require(ggplot2)){
  ggplot()+
    theme_bw()+
    theme(panel.spacing=grid::unit(0, "lines"))+
    facet_grid(part ~ Species)+
    coord_equal()+
    geom_abline(slope=1, intercept=0, color="grey")+
    geom_point(aes(
      Length, Width),
      data=iris.dims$nc)
}

It is clear from the plot above that Length is larger than Width for every measured flower part.

Do columns need to be sorted?

Consider the following wide data set:


TC <- data.table::data.table(
  age.treatment=c(1, 5),
  sex.control=c("M", "M"),
  sex.treatment=c("F", "F"),
  age.control=c(10, 50))

It is clear from the column names how the data should be grouped when they are converted to tall format. However the columns do not appear in regular order (age is before sex for treatment, but age is after sex for control), which causes a problem for stats and data.table:


input.list <- list(
  "nc"=nc::capture_melt_multiple(
    TC,
    column=".*?",
    "[.]",
    group=".*"),
  "cdata"=if(requireNamespace("cdata"))cdata::rowrecs_to_blocks(
    TC,
    controlTable=data.frame(
      group=c("treatment", "control"),
      age=c("age.treatment", "age.control"),
      sex=c("sex.treatment", "sex.control"),
      stringsAsFactors=FALSE)),
  "data.table"=data.table::melt(TC, measure.vars=patterns(
    age="age",
    sex="sex")),
  "stats"=stats::reshape(
    TC,
    varying=1:4,
    direction="long"),
  "tidyr"=if(requireNamespace("tidyr"))tidyr::pivot_longer(
    TC, 1:4,
    names_to=c(".value", "group"),
    names_sep="[.]"))
output.list <- list()
for(pkg in names(input.list)){
  df.or.null <- input.list[[pkg]]
  if(is.data.frame(df.or.null)){
    output.list[[pkg]] <- data.table::data.table(df.or.null)[order(age)]
  }
}
output.list
#> $nc
#>        group   age    sex
#>       <char> <num> <char>
#> 1: treatment     1      F
#> 2: treatment     5      F
#> 3:   control    10      M
#> 4:   control    50      M
#> 
#> $cdata
#>        group   age    sex
#>       <char> <num> <char>
#> 1: treatment     1      F
#> 2: treatment     5      F
#> 3:   control    10      M
#> 4:   control    50      M
#> 
#> $data.table
#>    variable   age    sex
#>      <fctr> <num> <char>
#> 1:        1     1      M
#> 2:        1     5      M
#> 3:        2    10      F
#> 4:        2    50      F
#> 
#> $stats
#>         time   age    sex    id
#>       <char> <num> <char> <int>
#> 1: treatment     1      M     1
#> 2: treatment     5      M     2
#> 3:   control    10      F     1
#> 4:   control    50      F     2
#> 
#> $tidyr
#>        group   age    sex
#>       <char> <num> <char>
#> 1: treatment     1      F
#> 2: treatment     5      F
#> 3:   control    10      M
#> 4:   control    50      M
sapply(output.list, function(DT)identical(DT$sex, c("F", "F", "M", "M")))
#>         nc      cdata data.table      stats      tidyr 
#>       TRUE       TRUE      FALSE      FALSE       TRUE

In conclusion, when the input column names to melt do not appear in the same order across groups or output columns, then the correct tall data can be computed using one of nc::capture_melt_multiple, tidyr::pivot_longer, cdata::rowrecs_to_blocks.

Melting into a single output column, who data

Another data set where it is useful to do column name pattern matching followed by melting is the World Health Organization data:


if(requireNamespace("tidyr")){
  data(who, package="tidyr")
}else{
  who <- data.frame(id=1, new_sp_m5564=2, newrel_f65=3)
}
names(who)
#>  [1] "country"      "iso2"         "iso3"         "year"         "new_sp_m014" 
#>  [6] "new_sp_m1524" "new_sp_m2534" "new_sp_m3544" "new_sp_m4554" "new_sp_m5564"
#> [11] "new_sp_m65"   "new_sp_f014"  "new_sp_f1524" "new_sp_f2534" "new_sp_f3544"
#> [16] "new_sp_f4554" "new_sp_f5564" "new_sp_f65"   "new_sn_m014"  "new_sn_m1524"
#> [21] "new_sn_m2534" "new_sn_m3544" "new_sn_m4554" "new_sn_m5564" "new_sn_m65"  
#> [26] "new_sn_f014"  "new_sn_f1524" "new_sn_f2534" "new_sn_f3544" "new_sn_f4554"
#> [31] "new_sn_f5564" "new_sn_f65"   "new_ep_m014"  "new_ep_m1524" "new_ep_m2534"
#> [36] "new_ep_m3544" "new_ep_m4554" "new_ep_m5564" "new_ep_m65"   "new_ep_f014" 
#> [41] "new_ep_f1524" "new_ep_f2534" "new_ep_f3544" "new_ep_f4554" "new_ep_f5564"
#> [46] "new_ep_f65"   "newrel_m014"  "newrel_m1524" "newrel_m2534" "newrel_m3544"
#> [51] "newrel_m4554" "newrel_m5564" "newrel_m65"   "newrel_f014"  "newrel_f1524"
#> [56] "newrel_f2534" "newrel_f3544" "newrel_f4554" "newrel_f5564" "newrel_f65"

Each column which starts with new has three distinct pieces of information encoded in its name: diagnosis type (e.g. sp or rel), gender (m or f), and age range (e.g. 5564 or 1524). We would like to use a regex to match these column names, then using the matching columns as measure.vars in a melt, then join the two results. The most convenient way to do that is via:


who.chr.list <- list(
  nc=nc::capture_melt_single(
    who,
    "new_?",
    diagnosis=".*",
    "_",
    gender=".",
    ages=".*"),
  tidyr=if(requireNamespace("tidyr"))tidyr::pivot_longer(
    who,
    new_sp_m014:newrel_f65,
    names_to=c("diagnosis", "gender", "ages"),
    names_pattern="new_?(.*)_(.)(.*)"))

Note the result includes additional column value which contains the melted data. There is also a column for each capture group in the specified pattern. The following example shows how to rename the value column, remove missing values, and use numeric type conversion functions:


who.pattern <- "new_?(.*)_(.)((0|[0-9]{2})([0-9]{0,2}))"
as.numeric.Inf <- function(y)ifelse(y=="", Inf, as.numeric(y))
who.typed.list <- list(
  nc=nc::capture_melt_single(
    who,
    "new_?",
    diagnosis=".*",
    "_",
    gender=".",
    ages=list(
      ymin.num="0|[0-9]{2}", as.numeric,
      ymax.num="[0-9]{0,2}", as.numeric.Inf),
    value.name="count",
    na.rm=TRUE),
  tidyr=if(requireNamespace("tidyr"))try(tidyr::pivot_longer(
    who,
    cols=grep(who.pattern, names(who)),
    names_transform=list(
      ymin.num=as.numeric,
      ymax.num=as.numeric.Inf),
    names_to=c("diagnosis", "gender", "ages", "ymin.num", "ymax.num"),
    names_pattern=who.pattern,
    values_drop_na=TRUE,
    values_to="count")))
str(who.typed.list)
#> List of 2
#>  $ nc   :Classes 'data.table' and 'data.frame':  76046 obs. of  10 variables:
#>   ..$ country  : chr [1:76046] "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
#>   ..$ iso2     : chr [1:76046] "AF" "AF" "AF" "AF" ...
#>   ..$ iso3     : chr [1:76046] "AFG" "AFG" "AFG" "AFG" ...
#>   ..$ year     : num [1:76046] 1997 1998 1999 2000 2001 ...
#>   ..$ diagnosis: chr [1:76046] "sp" "sp" "sp" "sp" ...
#>   ..$ gender   : chr [1:76046] "m" "m" "m" "m" ...
#>   ..$ ages     : chr [1:76046] "014" "014" "014" "014" ...
#>   ..$ ymin.num : num [1:76046] 0 0 0 0 0 0 0 0 0 0 ...
#>   ..$ ymax.num : num [1:76046] 14 14 14 14 14 14 14 14 14 14 ...
#>   ..$ count    : num [1:76046] 0 30 8 52 129 90 127 139 151 193 ...
#>   ..- attr(*, ".internal.selfref")=<externalptr> 
#>  $ tidyr: tibble [76,046 × 10] (S3: tbl_df/tbl/data.frame)
#>   ..$ country  : chr [1:76046] "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
#>   ..$ iso2     : chr [1:76046] "AF" "AF" "AF" "AF" ...
#>   ..$ iso3     : chr [1:76046] "AFG" "AFG" "AFG" "AFG" ...
#>   ..$ year     : num [1:76046] 1997 1997 1997 1997 1997 ...
#>   ..$ diagnosis: chr [1:76046] "sp" "sp" "sp" "sp" ...
#>   ..$ gender   : chr [1:76046] "m" "m" "m" "m" ...
#>   ..$ ages     : chr [1:76046] "014" "1524" "2534" "3544" ...
#>   ..$ ymin.num : num [1:76046] 0 15 25 35 45 55 65 0 15 25 ...
#>   ..$ ymax.num : num [1:76046] 14 24 34 44 54 ...
#>   ..$ count    : num [1:76046] 0 10 6 3 5 2 0 5 38 36 ...

The result above shows that nc::capture_melt_single (1) makes it easier to define complex patterns (2) supports type conversion without a post-processing step, and (3) reduces repetition in user code. There are several sources of repetition in tidyr code:

Other packages for doing this include:


if(requireNamespace("tidyr")){
  gather.result <- tidyr::gather(
    who,
    "variable",
    "count",
    grep(who.pattern, names(who)),
    na.rm=TRUE)
  extract.result <- tidyr::extract(
    gather.result,
    "variable",
    c("diagnosis", "gender", "ages", "ymin.int", "ymax.int"),
    who.pattern,
    convert=TRUE)
  transform.result <- base::transform(
    extract.result,
    ymin.num=as.numeric(ymin.int),
    ymax.num=ifelse(is.na(ymax.int), Inf, as.numeric(ymax.int)))
  str(transform.result)
}
#> 'data.frame':    76046 obs. of  12 variables:
#>  $ country  : chr  "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
#>  $ iso2     : chr  "AF" "AF" "AF" "AF" ...
#>  $ iso3     : chr  "AFG" "AFG" "AFG" "AFG" ...
#>  $ year     : num  1997 1998 1999 2000 2001 ...
#>  $ diagnosis: chr  "sp" "sp" "sp" "sp" ...
#>  $ gender   : chr  "m" "m" "m" "m" ...
#>  $ ages     : int  14 14 14 14 14 14 14 14 14 14 ...
#>  $ ymin.int : int  0 0 0 0 0 0 0 0 0 0 ...
#>  $ ymax.int : int  14 14 14 14 14 14 14 14 14 14 ...
#>  $ count    : num  0 30 8 52 129 90 127 139 151 193 ...
#>  $ ymin.num : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ ymax.num : num  14 14 14 14 14 14 14 14 14 14 ...

Note that tidyr::gather requires two post-processing steps, which cause the same two types of repetition as tidyr::pivot_longer:

The reshape2 package suffers from the same two issues:


reshape2.result <- if(requireNamespace("reshape2")){
  reshape2:::melt.data.frame(
    who,
    measure.vars=grep(who.pattern, names(who)),
    na.rm=TRUE,
    value.name="count")
}
#> Loading required namespace: reshape2

Interestingly, data.table::patterns can be used to avoid repeating the data set name, who. However it supports neither type conversion nor regex capture groups.


dt.result <- data.table::melt.data.table(
  data.table(who),
  measure.vars=patterns(who.pattern),
  na.rm=TRUE,
  value.name="count")

Neither cdata nor stats provide an na.rm option:


who.df <- data.frame(who)
is.varying <- grepl(who.pattern, names(who))
names(who.df)[is.varying] <- paste0("count.", names(who)[is.varying])
stats.result <- stats::reshape(
  who.df,
  direction="long",
  timevar="variable",
  varying=is.varying)

if(requireNamespace("cdata")){
  cdata.result <- cdata::rowrecs_to_blocks(
    who, 
    cdata::build_unpivot_control(
      "variable",
      "count",
      grep(who.pattern, names(who), value=TRUE)),
    columnsToCopy=grep(who.pattern, names(who), value=TRUE, invert=TRUE))
}

Melting a wider iris back to original


## Example 1: melting a wider iris data back to original.
library(data.table)
iris.dt <- data.table(
  i=1:nrow(iris),
  iris[,1:4],
  Species=paste(iris$Species))
print(iris.dt)
#>          i Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>      <int>        <num>       <num>        <num>       <num>    <char>
#>   1:     1          5.1         3.5          1.4         0.2    setosa
#>   2:     2          4.9         3.0          1.4         0.2    setosa
#>   3:     3          4.7         3.2          1.3         0.2    setosa
#>   4:     4          4.6         3.1          1.5         0.2    setosa
#>   5:     5          5.0         3.6          1.4         0.2    setosa
#>  ---                                                                  
#> 146:   146          6.7         3.0          5.2         2.3 virginica
#> 147:   147          6.3         2.5          5.0         1.9 virginica
#> 148:   148          6.5         3.0          5.2         2.0 virginica
#> 149:   149          6.2         3.4          5.4         2.3 virginica
#> 150:   150          5.9         3.0          5.1         1.8 virginica

## what if we had two observations on each row?
set.seed(1)
iris.rand <- iris.dt[sample(.N)]
iris.wide <- cbind(treatment=iris.rand[1:75], control=iris.rand[76:150])
print(iris.wide, topn=2, nrows=10)
#>     treatment.i treatment.Sepal.Length treatment.Sepal.Width
#>           <int>                  <num>                 <num>
#>  1:          68                    5.8                   2.7
#>  2:         129                    6.4                   2.8
#> ---                                                         
#> 74:          91                    5.5                   2.6
#> 75:          64                    6.1                   2.9
#>     treatment.Petal.Length treatment.Petal.Width treatment.Species control.i
#>                      <num>                 <num>            <char>     <int>
#>  1:                    4.1                   1.0        versicolor        60
#>  2:                    5.6                   2.1         virginica       113
#> ---                                                                         
#> 74:                    4.4                   1.2        versicolor        57
#> 75:                    4.7                   1.4        versicolor        72
#>     control.Sepal.Length control.Sepal.Width control.Petal.Length
#>                    <num>               <num>                <num>
#>  1:                  5.2                 2.7                  3.9
#>  2:                  6.8                 3.0                  5.5
#> ---                                                              
#> 74:                  6.3                 3.3                  4.7
#> 75:                  6.1                 2.8                  4.0
#>     control.Petal.Width control.Species
#>                   <num>          <char>
#>  1:                 1.4      versicolor
#>  2:                 2.1       virginica
#> ---                                    
#> 74:                 1.6      versicolor
#> 75:                 1.3      versicolor

## This is the usual data.table syntax for getting the original iris back.
iris.melted <- melt(iris.wide, value.factor=TRUE, measure.vars = patterns(
  i="i$",
  Sepal.Length="Sepal.Length$",
  Sepal.Width="Sepal.Width$",
  Petal.Length="Petal.Length$",
  Petal.Width="Petal.Width$",
  Species="Species$"))
identical(iris.melted[order(i), names(iris.dt), with=FALSE], iris.dt)
#> [1] TRUE

## nc can do the same thing -- you must define an R argument named
## column, and another named argument which identifies each group.
(nc.melted <- nc::capture_melt_multiple(
  iris.wide,
  group="[^.]+",
  "[.]",
  column=".*"))
#>          group Petal.Length Petal.Width Sepal.Length Sepal.Width    Species
#>         <char>        <num>       <num>        <num>       <num>     <char>
#>   1:   control          3.9         1.4          5.2         2.7 versicolor
#>   2:   control          5.5         2.1          6.8         3.0  virginica
#>   3:   control          5.6         1.4          6.1         2.6  virginica
#>   4:   control          1.5         0.1          4.9         3.1     setosa
#>   5:   control          1.4         0.2          5.1         3.5     setosa
#>  ---                                                                       
#> 146: treatment          1.6         0.2          4.8         3.1     setosa
#> 147: treatment          1.3         0.4          5.4         3.9     setosa
#> 148: treatment          5.4         2.1          6.9         3.1  virginica
#> 149: treatment          4.4         1.2          5.5         2.6 versicolor
#> 150: treatment          4.7         1.4          6.1         2.9 versicolor
#>          i
#>      <int>
#>   1:    60
#>   2:   113
#>   3:   135
#>   4:    10
#>   5:     1
#>  ---      
#> 146:    31
#> 147:    17
#> 148:   140
#> 149:    91
#> 150:    64
identical(nc.melted[order(i), names(iris.dt), with=FALSE], iris.dt)
#> [1] TRUE

## This is how we do it using stats::reshape.
iris.wide.df <- data.frame(iris.wide)
names(iris.wide.df) <- sub("(.*?)[.](.*)", "\\2_\\1", names(iris.wide))
iris.reshaped <- stats::reshape(
  iris.wide.df,
  direction="long",
  timevar="group",
  varying=names(iris.wide.df),
  sep="_")
identical(data.table(iris.reshaped[, names(iris.dt)])[order(i)], iris.dt)
#> [1] TRUE

## get the parts columns and groups -- is there any difference
## between groups? of course not!
parts.wide <- nc::capture_melt_multiple(
  iris.wide,
  group=".*?",
  "[.]",
  column=".*?",
  "[.]",
  dim=".*")
if(require("ggplot2")){
  ggplot()+
    theme_bw()+
    theme(panel.spacing=grid::unit(0, "lines"))+
    facet_grid(dim ~ group)+
    coord_equal()+
    geom_abline(slope=1, intercept=0, color="grey")+
    geom_point(aes(
      Petal, Sepal),
      data=parts.wide)
}