小编gre*_*Bag的帖子

如何在给定日期之前dcast一个具有缺失值的data.table

 DT <- data.table(Id = c(1, 1, 1, 1, 10, 100, 100, 101, 101, 101), 
                 Date = as.Date(c("1997-01-01", "1997-01-02", "1997-01-03", "1997-01-04", 
                                  "1997-01-02", "1997-01-02", "1997-01-04", "1997-01-03", 
                                  "1997-01-04", "1997-01-04")), 
                 Price = c(29, 25, 14, 26, 30, 16, 13, 62, 12,  6), 
                 IsFirst = c(T,F,F,F,T,T,F,T,F,F))
Run Code Online (Sandbox Code Playgroud)

Id:客户ID; 日期:交易日期; 价格:交易金额; isFirst:TRUE是交易是客户第一.每个客户有1个IsFirst == TRUE,[0,Inf] IsFirst == FALSE.

>DT
     Id       Date Price IsFirst
 1:   1 1997-01-01    29    TRUE
 2:   1 1997-01-02    25   FALSE
 3:   1 1997-01-03    14   FALSE
 4:   1 1997-01-04    26   FALSE
 5:  10 1997-01-02    30    TRUE …
Run Code Online (Sandbox Code Playgroud)

optimization r data.table

5
推荐指数
1
解决办法
1329
查看次数

cbind来自多个data.tables的特定列有效

我有一个data.tables列表,我需要cbind,但是,我只需要最后的X列.

我的数据结构如下:

DT.1 <- data.table(x=c(1,1), y = c("a","a"), v1 = c(1,2), v2 = c(3,4))
DT.2 <- data.table(x=c(1,1), y = c("a","a"), v3 = c(5,6))
DT.3 <- data.table(x=c(1,1), y = c("a","a"), v4 = c(7,8), v5 = c(9,10), v6 = c(11,12))

DT.list <- list(DT.1, DT.2, DT.3)
>DT.list
    [[1]]
   x y v1 v2
1: 1 a  1  3
2: 1 a  2  4

[[2]]
   x y v3
1: 1 a  5
2: 1 a  6

[[3]]
   x y v4 v5 v6
1: 1 a …
Run Code Online (Sandbox Code Playgroud)

r cbind data.table

5
推荐指数
1
解决办法
3203
查看次数

如何转换data.table的多个列和值?

我的数据结构如下:

DT <- data.table(Id = c(1, 1, 1, 1, 10, 100, 100, 101, 101, 101), 
                 Date = as.Date(c("1997-01-01", "1997-01-02", "1997-01-03", "1997-01-04", 
                                  "1997-01-02", "1997-01-02", "1997-01-04", "1997-01-03", 
                                  "1997-01-04", "1997-01-04")),
                 group = c(1,1,1,1,1,2,2,2,2,2),
                 Price.1 = c(29, 25, 14, 26, 30, 16, 13, 62, 12,  6), 
                 Price.2 = c(4, 5, 6, 6, 8, 2, 3, 5, 7, 8))

>DT
     Id       Date group Price.1 Price.2
 1:   1 1997-01-01     1      29       4
 2:   1 1997-01-02     1      25       5
 3:   1 1997-01-03     1      14       6
 4:   1 …
Run Code Online (Sandbox Code Playgroud)

r data.table

4
推荐指数
1
解决办法
4487
查看次数

如何有效地按组聚合多个data.table列,一次一个N,其中N是可变的

我使用的数据集结构如下:

library("data.table")

   dt <- data.table(Id = c(1,2,3,4,5,6,7,8), "01.01.2005" = c(10,0,0,0,5,7,7,0),
 "02.01.2005" = c(4,5,7,0,0,0,9,9), "03.01.2005" = c(2,3,3,6,7,77,45,0), 
"04.01.2005" = c(0,0,0,0,0,0,0,1), "05.01.2005" = c(0,1,1,1,1,0,0,2), 
"06.01.2005" = c(45,15,45,54,5,76,67,10), "07.01.2005" = c(0,0,0,0,466,21,832,54), 
"08.01.2005" = c(2,3,734,2,3,4,5,6))

>dt
       Id 01.01.2005 02.01.2005 03.01.2005 04.01.2005 05.01.2005 06.01.2005 07.01.2005 08.01.2005
    1:  1         10          4          2          0          0         45          0          2
    2:  2          0          5          3          0          1         15          0          3
    3:  3          0          7          3          0          1         45          0        734
    4:  4          0          0          6          0          1         54          0 …
Run Code Online (Sandbox Code Playgroud)

optimization r data.table

2
推荐指数
1
解决办法
102
查看次数

标签 统计

data.table ×4

r ×4

optimization ×2

cbind ×1