Skip to content

measure() multiple.keyword not used with single groups #5065

@keatingw

Description

@keatingw

First up, thank you very much for implementing measure() - it's a fantastic addition to the reshaping tools.

When using the multiple keyword (value.name by default) it's ignored in the case where only one value is matched in that capture group. This might be intended behaviour, so feel free to close this issue if so.

The documentation for measure() doesn't seem to suggest this behaviour is deliberate, but it might just be a point of clarification to avoid surprises (e.g. describing when measure overrides the value name argument and vice versa):

multiple.keyword | A string, if used as a group name, then measure returns a list and melt returns multiple value columns (with names defined by the unique values in that group). Otherwise if the string not used as a group name, then measure returns a vector and melt returns a single value column.

# Minimal reproducible example

library(data.table)
(x = as.data.table(iris)[1])
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species                                                                                                                        
# 1:          5.1         3.5          1.4         0.2  setosa

# Melt with multiple groups and value.name keyword, behaves as expected
melt(
  x,
  id.vars = "Species",
  measure.vars = measure(value.name, measurement, pattern = "(Petal|Sepal)\\.(Length|Width)")
)
#    Species measurement Sepal Petal                                                                                                                                                  
# 1:  setosa      Length   5.1   1.4                                                                                                                                                  
# 2:  setosa       Width   3.5   0.2 

# Melt with keyword matching only Sepal columns, and melt reverts to using the value.name argument
melt(
  x,
  id.vars = "Species",
  measure.vars = measure(value.name, measurement, pattern = "(Petal)\\.(Length|Width)")
)
#    Species measurement value                                                                                                                                                        
# 1:  setosa      Length   1.4                                                                                                                                                        
# 2:  setosa       Width   0.2 

# Output of sessionInfo()

r$> sessionInfo()                                                                                                                                                                   
R version 4.1.0 (2021-05-18)                                                                                                                                                        
Platform: x86_64-w64-mingw32/x64 (64-bit)                                                                                                                                           
Running under: Windows 10 x64 (build 19042)                                                                                                                                         
                                                                                                                                                                                    
Matrix products: default                                                                                                                                                            
                                                                                                                                                                                    
locale:                                                                                                                                                                             
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                       LC_TIME=English_Australia.1252      
                                                                                                                                                                                    
attached base packages:                                                                                                                                                             
[1] stats     graphics  grDevices utils     datasets  methods   base                                                                                                                
                                                                                                                                                                                    
other attached packages:                                                                                                                                                            
[1] data.table_1.14.1                                                                                                                                                               
                                                                                                                                                                                    
loaded via a namespace (and not attached):                                                                                                                                          
[1] compiler_4.1.0    

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions