I'd like to create a function "startswith' to be used within brackets in data.table. It should return a character vector containing the column names that begin with the character provided. For example
DT <- data.table(x=1, y=2, z1=1, z2=2)
# the syntax DT[, startswith("z")] is equivalent to
DT[, .(z1, z2)]
# returns
z1 z2
1: 1 2
I'm familiar with grep to search for text expressions, but am having trouble finding a way to refer to the column names of DT from within the brackets. One solution I attempted was to use ls() and the environment associated with DT to list all of the columns in DT, but I haven't found a way to refer to this environment from within the brackets.
The goal is to create a wrapper for grep to be used as a convenience function. I don't want to have to specify the DT from within the brackets.