0

I am calling a function that returns a data.table with one single column.

dt <- data.table(Cmpd.Batch=(1:10), Mass=101:105, REP=1:2, CONC=rep(1:2, each=5), REPEAT=1)
dt[, makeCalcResHeader(.SD), by="CONC", .SDcols=c("Cmpd.Batch", "Mass", "REP", "CONC", "REPEAT")]

makeCalcResHeader <- function(dt){
    headDT <- data.table(c("Text", paste0("curve id[", dt$CONC[1], "uM]")))
    dataDT <- data.table(unique(paste(dt$Cmpd.Batch, dt$Mass, dt$REP, dt$CONC, dt$REPEAT, sep="-")))
    return(rbind(headDT, dataDT))
  }

But I want each returned data.table (for each CONC/REPEAT group) to be it's own separate column so that each time the function runs a column is appended to the original data.table.

Each 'section' should be its own separate column.

Is there a data.table way to do this?

smci
  • 32,567
  • 20
  • 113
  • 146
James
  • 1,447
  • 3
  • 16
  • 30
  • 1
    Side note: For efficiency, your function should return a list, not a data.table. This is currently number 4 here: https://github.com/Rdatatable/data.table/wiki/Do%27s-and-Don%27ts – Frank May 21 '15 at 18:28
  • 3
    I guess you have heard before about minimal reproducible examples, but here is a reference: http://stackoverflow.com/a/28481250/1191259 – Frank May 21 '15 at 18:32

0 Answers0