<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Will become more natural when <a href="https://github.com/Rdatatable/data.table/issues/495">https://github.com/Rdatatable/data.table/issues/495</a> is fixed. I’ll work on this at some point for v1.9.8. I don’t have a time frame yet.</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">First, for both cases, I’d suggest using `Map()`. Second, as a temporary solution, I’d suggest using `mget()`.</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><span style="font-family: 'helvetica Neue', helvetica;">#when there is no by it is easy </span><br style="font-family: 'helvetica Neue', helvetica;"><span style="font-family: 'helvetica Neue', helvetica;">DT[, (rCols) := Map(f, mget(xCols), list(x4))] </span><br style="font-family: 'helvetica Neue', helvetica;"><br style="font-family: 'helvetica Neue', helvetica;"><span style="font-family: 'helvetica Neue', helvetica;">#when there is a by it fails (offcourse DT$x4 is too big) </span><br style="font-family: 'helvetica Neue', helvetica;"><span style="font-family: 'helvetica Neue', helvetica;">DT[, (rCols) := Map(f, mget(xCols), list(x4)), by=y] </span><br style="font-family: 'helvetica Neue', helvetica;"><br style="font-family: 'helvetica Neue', helvetica;"></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">When #495 is fixed, `mget(xCols)` can be replaced with `.SD` along with `.SDcols = xCols`.</div> <br> <div id="bloop_sign_1444822828706033920" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Arun</div></div> <br><p class="airmail_on">On 14 October 2015 at 13:33:37, statquant3 (<a href="mailto:statquant@outlook.com">statquant@outlook.com</a>) wrote:</p> <blockquote type="cite" class="clean_bq"><span><div><div></div><div>Hello, I am looking to  
<br> * update several columns by  
<br> * applying a function f (to each of those columns) that would use those
<br>columns AND another one.
<br>
<br>If there is no "by" I make it work
<br>How can I do the same with a "by" :
<br>
<br>Bellow an example (as I can't be very clear)
<br>
<br>#data setup
<br>library(data.table)
<br>set.seed(1)
<br>N <- 101
<br>DT <-
<br>data.table(x1=rnorm(N),x2=rnorm(N),x3=rnorm(N),x4=rnorm(N),y=letters[sample(5,size=N,replace=T)])
<br>
<br>#function to be applied
<br>f <- function(x,y){return( frank(x/y,na.last='keep') )}
<br>#column names
<br>xCols <- paste0('x',1:3)
<br>rCols <- paste0('r',1:3)
<br>
<br>#when there is no by it is easy
<br>DT[,(rCols):=lapply(FUN=f,X=.SD,y=DT$x4),.SDcols=xCols]
<br>
<br>#when there is a by it fails (offcourse DT$x4 is too big)
<br>DT[,(rCols):=lapply(FUN=f,X=.SD,y=DT$x4),.SDcols=xCols,by=.(y)]
<br>
<br>
<br>
<br>
<br>--
<br>View this message in context: http://r.789695.n4.nabble.com/How-can-I-apply-a-function-of-2-columns-to-multiple-other-columns-with-a-by-clause-tp4713576.html
<br>Sent from the datatable-help mailing list archive at Nabble.com.
<br>_______________________________________________
<br>datatable-help mailing list
<br>datatable-help@lists.r-forge.r-project.org
<br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
<br></div></div></span></blockquote></body></html>