Search code examples
rdata.tableparameterization

In data.table how can I supply a vector of characters to c() with some explicitly and others by reference?


In a normal one off script way I have a data.table with headings:

Date | Time | Value

Which then has the Date and Time pasted together to be parsed as POSIX later on.

The problem is in trying to parametrise (I think that's the right word) the process into a function, I can't guarantee that Date will always be explicitly "Date". It could easily be date, DATE, rdate (for some reason), or really anything.

Right now my function produces:

Date | Time | Value | DateTime

.. but as I've mentioned, DateTime could easily be rDateTime. Time and Valueare set elsewhere however, and will not change.

EDIT Currently the function captures the actual character string from the function inputs.

function(hhDT, colDate = "rdate")

The question is:

In setcolorder, where the column headers are supplied as characters in the c() function like so:

setcolorder(fooDT, c("col1", "col2", "col3",....))

What is the 'correct' way to supply the argument to the c() when it is partly known and partly user defined, and captured in the character object colDate?

i.e.

setcolorder(fooDT, c(colDate, "Time", result of paste(colDate, "Time"), "Value"))

Where colDate is a character string referred to with argument colDate, and result of ... is the result of that pseudo code i.e. rDateTime or dateTime or randomstringTime.


Solution

  • Are the column positions constant? if so you should use it.
    If you cannot be sure they will be constant in future you can easily built in the check for Time position as 2nd col and Value position as 3rd column. If any not true just raise the error.
    Do not focus on setcolorder but on c to produce expected character vector.
    Your solution is almost working, you can nest paste inside c:

    mysetcolorder <- function(DT, colDate){
        stopifnot(is.data.table(DT), is.character(colDate), colDate %in% names(DT))
        setcolorder(DT, c(colDate, "Time", paste0(colDate, "Time"), "Value"))
    }
    

    If it doesn't answer your question you can take a laptop for tomorrows R user group and we will make it work and update the answer :)