data.table Advanced Technics And Examples | Data Science With R

目錄

Original Post

data.table Advanced Technics And Examples | Data Science With R


data.table是R語言的熱門套件,能夠快速處理大量資料,效率高於dplyr,且得利於語法結構的精巧設計,data.table更容易寫出版排整齊的程式。這篇文章會討論data.table的進階技巧,在Cheat Sheat上也不一定找得到。

.GRP – Add index for each group

dt <- data.table(C1 = c("A", "B", "C", "B", "A"),
                 C2 = c(1, 2, 3, 2, 1))

dt2 <- dt[, INDEX := .GRP, by = .(C1)]
   C1 C2 INDEX
1:  A  1     1
2:  B  2     2
3:  C  3     3
4:  B  2     2
5:  A  1     1

Subset with row index and column index

dt <- data.table(C1 = 1:3,
                 C2 = 101:103,
                 C3 = 901:903)

dt_row <- dt[1:2, ]
dt_col <- dt[, 2:3]
dt_row_col <- dt[1:2, 2:3]
> dt_row
   C1  C2  C3
1:  1 101 901
2:  2 102 902
> dt_col
    C2  C3
1: 101 901
2: 102 902
3: 103 903
> dt_row_col
    C2  C3
1: 101 901
2: 102 902
Content Protection by DMCA.com
A
Aron

Leave a Reply

Your email address will not be published. Required fields are marked *