Yeah, I would use mapply pretty much the same way: dt[, mapply(sub, pattern = shift(col1, type="lead", fill=""), replacement = "", x = col1, USE.NAMES = FALSE)]
I expect it to be faster than a loop, since `dt[]` has some overhead that you would incur for each row of a loop. Once you get used to mapply/Map, it won't seem so hard to read, I think. On Mon, Oct 19, 2015 at 11:28 AM, Nicolas Paris <[email protected]> wrote: > Thanks all, > > Actualy my use case is a little different > > > dt <- data.table(col1=c("FOOBARBAZ","BARBAZ","BAZ")) > col1 > 1: FOOBARBAZ > 2: BARBAZ > 3: BAZ > > > What I want to get is : > > data.table(col1=c("FOO","BAR","BAZ")) > col1 > 1: FOO > 2: BAR > 3: BAZ > > (I remove next row from actual row) > > This works : > dt[,col3:=mapply(function(x,y){gsub(x,"",y,fixed=T)}, > shift(col1,fill=" ",type="lead"), > col1)] > > > Have you in mind a better solution ? Will it be faster than loop (yet this > is less readable)? > > Thanks again > > > > > 2015-10-19 16:12 GMT+02:00 Frank Erickson <[email protected]>: > >> I think `shift` is the best option: >> >> dt <-data.table(col1=c(1,2,3)) >> dt[, col3 := shift(col1, type="lag") > 2] >> >> On Mon, Oct 19, 2015 at 10:10 AM, jim holtman <[email protected]> wrote: >> >>> does this do what you want: >>> >>> > dt <-data.table(col1=c(1,2,3)) >>> > dt >>> col1 >>> 1: 1 >>> 2: 2 >>> 3: 3 >>> > dt[2:nrow(dt), col3:=dt[1:(nrow(dt) - 1), list(col1)]>2] >>> > dt >>> col1 col3 >>> 1: 1 NA >>> 2: 2 FALSE >>> 3: 3 FALSE >>> >>> >>> >>> Jim Holtman >>> Data Munger Guru >>> >>> What is the problem that you are trying to solve? >>> Tell me what you want to do, not how you want to do it. >>> >>> On Mon, Oct 19, 2015 at 9:59 AM, Nicolas Paris <[email protected]> >>> wrote: >>> >>>> Hello, >>>> >>>> I wonder if there is a way in data.table (or more generaly in R) to >>>> work on previous row without loops >>>> E.G. something equivalent to : >>>> >>>> dt <-data.table(col1=c(1,2,3)) >>>> for (i in 2:nrow(dt)) >>>> { >>>> dt[i,col3:=dt[i-1,list(col1)]>2] >>>> } >>>> >>>> Thanks a lot ! >>>> >>>> _______________________________________________ >>>> datatable-help mailing list >>>> [email protected] >>>> >>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help >>>> >>> >>> >>> _______________________________________________ >>> datatable-help mailing list >>> [email protected] >>> >>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help >>> >> >> > > _______________________________________________ > datatable-help mailing list > [email protected] > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help >
_______________________________________________ datatable-help mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
