[datatable-help] Fail to add new columns within a function
Matt Dowle
mdowle at mdowle.plus.com
Wed Dec 18 10:58:46 CET 2013
Why are you doing this iteratively? Can't you load all the files into a
list, rbindlist and then reshape?
On 18/12/13 09:04, Huashan Chen wrote:
> OK, here is the complete code with some mock functions from my example.
>
> # data: data.table object
> # fn: a filename to read data from
> merge_data<-function(fn, data){
> fs<-getSavedata(fn) # read as data.frame
> if (is.null(fs)) stop('Empty data file')
>
> # return a character vector of variable names which are to merged, some
> variables in fs will not be merged to DT
> newvars<-selectVars(names(fs))
> stopifnot(length(newvars) > 0)
>
> # determine which rows to use
> caseid<-someCustomFunc(fs)
>
> add_var<-function(varname){
> data[caseid, `:=`(eval(substitute(varname)), fs[,
> toupper(eval(substitute(varname)))]), with=F]
> }
> invisible(sapply(newvars, add_var))
> }
>
> # calling function
> merge_data('some file', DT)
> DT # display the updated results
>
>
> In this case, I think a warning from merge_data() when the quota is reached
> would be appreciated. Of couse, I could have added a check within the
> function to avoid unintended action.
>
> if (truelength(data) <= ncol(data) + 64L) stop('increase colunmn quota
> using alloc.col() before calling this function.')
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Fail-to-add-new-columns-within-a-function-tp4682173p4682393.html
> Sent from the datatable-help mailing list archive at Nabble.com.
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
More information about the datatable-help
mailing list