Tag all duplicate rows in R as in Stata

后端 未结 2 1119
眼角桃花
眼角桃花 2021-01-13 02:55

Following up from my question here, I am trying to replicate in R the functionality of the Stata command duplicates tag, which allows me to tag all the rows of

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-13 03:31

    I'll answer your third question here.. (I think the first question is more or less answered in your other post).

    ## Assuming DT is your data.table
    DT[, dupvar := 1L*(.N > 1L), by=c(indexVars)]
    

    := adds a new column dupvar by reference (and is therefore very fast because no copies are made). .N is a special variable within data.table, that provides the number of observations that belong to each group (here, for every f1,f2,f3,f4).

    Take your time and go through ?data.table (and run the examples there) to understand the usage. It'll save you a lot of time later on.

    So, basically, we group by indexVars, check if .N > 1L and if it's the case, it'd return TRUE. We multiply by 1L to return an integer instead of logical value.

    If you require, you can also sort it by the by-columns using setkey.


    From the next version on (currently implemented in v1.9.3 - development version), there's also a function setorder that's exported that just sorts the data.table by reference, without setting keys. It also can sort in ascending or descending order. (Note that setkey always sorts in ascending order only).

    That is, in the next version you can do:

    setorder(DT, f1, f2, f3, f4)
    ## or equivalently
    setorderv(DT, c("f1", "f2", "f3", "f4"))
    

    In addition, the usage DT[order(...)] is also optimised internally to use data.table's fast ordering. That is, DT[order(...)] is detected internally and changed to DT[forder(DT, ...)] which is incredibly faster than base's order. So, if you don't want to change it by reference, and want to assign the sorted data.table on to another variable, you can just do:

    DT_sorted <- DT[order(f1, f2, f3, f4)] ## internally optimised for speed
                                           ## but still copies!
    

    HTH

提交回复
热议问题