recleev · GitHub

I wanted to do something like this

library(cdata)
control_table <- qchar_frame(
  Part   , Measure , Value        |
  "Sepal", "Length", Sepal.Length |
  "Sepal", "Width" , Sepal.Width  |
  "Petal", "Length", Petal.Length |
  "Petal", "Width" , Petal.Width
)
rowrecs_to_blocks(iris, control_table)

But I get this error

Error in rowrecs_to_blocks.default(iris, control_table) :
  cdata::rowrecs_to_blocks all control table group ids must be distinct

checkControlTable() assumes that the first column is always and the only id column, so when the first column does not have distinct values it throws an error.

I think cdata should be able to support a combination of multiple columns as ids. In my example above, the combination of Part and Measure constitutes a group id. Maybe an extra argument to specify the id cols in the control table can make this work?

Something like this?

rowrecs_to_blocks(iris, control_table, keyColumns = c("Part", "Measure"))

keyColumns (like the one in blocks_to_rowrecs()) can also take a vector of col index to specify the columns to take as group ids. Default should be 1 to keep current behavior.

Here is a work around with {data.table}

control_table_2 <- qchar_frame(
  Part.Measure  , Value        |
  "Sepal.Length", Sepal.Length |
  "Sepal.Width" , Sepal.Width  |
  "Petal.Length", Petal.Length |
  "Petal.Width" , Petal.Width
)
iris_long <- rowrecs_to_blocks(iris, control_table_2)
library(data.table)
iris_long <- as.data.table(iris_long)
iris_long[, c("Part", "Measure") := tstrsplit(Part.Measure, split = "\\.")]
# > iris_long
# Part.Measure      Value  Part  Measure
# 1: Sepal.Length     5.1 Sepal  Length
# 2: Sepal.Width      3.5 Sepal  Width
# 3: Petal.Length     1.4 Petal  Length
# 4: Petal.Width      0.2 Petal  Width
# 5: Sepal.Length     4.9 Sepal  Length
# ---
# 596: Petal.Width    2.3 Petal  Width
# 597: Sepal.Length   5.9 Sepal  Length
# 598: Sepal.Width    3.0 Sepal  Width
# 599: Petal.Length   5.1 Petal  Length
# 600: Petal.Width    1.8 Petal  Width

Splitting columns is easy, but I was just wondering if it can be avoided.

Read the original on github.com ↗