r - data.table assignment by reference within function -
this question has answer here:
i have (big-ish) data.table
. , want same thing many of columns. say,
dt <- data.table( ltr = letters[1:5] ) func <- function( dt ){ cols <- colnames( dt ) # columns apply op dt[ , (cols) := lapply( .sd, toupper ), .sdcols = cols ] return( dt ) }
the function func
converts characters upper case columns. try
func( dt )
i error
error in `:=`((cols), lapply(.sd, toupper)) : check is.data.table(dt) == true. otherwise, := , `:=`(...) defined use in j, once , in particular ways. see help(":=").
i'm still not sure causes error , have read help(":=")
, faqs , other (so) sources. help?
note: should have given more details context. func
function written in package. makes difference
the short answer make sure package data.table aware. see @arun's comment.
note using devtools::use_package( "data.table", pkg = "." )
add imports
field in description
not explicitly import( data.table )
in namespace. such, including data.table
in depends
field -- that's first option matthew outlines in post -- may more foolproof.
Comments
Post a Comment