This is a display method for data.frames to show ragged key/grouping variables, similar to ftable

ragged(indt, keys, blank = "")

# S3 method for ragged
print(x, ...)

Arguments

indt

The input data.frame or data.table

keys

The variables to be used as keys or grouping variables

blank

The character to print to show nesting. Defaults to "".

x

The object to be printed.

...

Not used.

Value

A list with a "ragged" object and the sorted data.table. The custom print method displays the "ragged" result, but allows further use of data.table.

References

https://stackoverflow.com/q/41324110/1270695

See also

Author

Ananda Mahto

Examples

before= data.frame(C1= c(rep("A", 5), rep("L", 2)), C2= c("B", rep("E", 3), rep("K", 2), "L"), C3= c("C", "F", rep("H", 5)), C4= c("D", "G", "I", rep("J", 4)), stringsAsFactors = FALSE) ragged(before, c("C1", "C2"))
#> C1 C2 C3 C4 #> 1: A B C D #> 2: E F G #> 3: H I #> 4: H J #> 5: K H J #> 6: L K H J #> 7: L H J
ragged(before, names(before), ":")
#> C1 C2 C3 C4 #> 1: A B C D #> 2: : E F G #> 3: : : H I #> 4: : : : J #> 5: : K H J #> 6: L K H J #> 7: : L H J
ragged(head(ggplot2::diamonds, 30), c("cut", "color"), ":")[, mean(price), .(cut, color)]
#> cut color V1 #> 1: Fair E 337.0000 #> 2: Good E 327.0000 #> 3: : I 351.0000 #> 4: : J 344.0000 #> 5: Very Good D 357.0000 #> 6: : E 352.0000 #> 7: : F 357.0000 #> 8: : G 354.0000 #> 9: : H 342.6667 #> 10: : I 336.0000 #> 11: : J 350.0000 #> 12: Premium E 338.6667 #> 13: : F 342.0000 #> 14: : I 344.5000 #> 15: Ideal E 326.0000 #> 16: : I 348.0000 #> 17: : J 342.0000