【问题标题】:Create a seq in data.table based on levels in a factor in R [duplicate]根据 R 中一个因子的级别在 data.table 中创建一个序列 [重复]
【发布时间】:2015-01-08 06:02:21
【问题描述】:

我有一个如下的data.table

DT <- structure(list(Seq = c(1, 2, 3, 5, 7, 9, 11, 15, 23, 67, 1, 3, 
4, 5, 9, 3, 4, 6), Tpe = c("U", "U", "U", "U", "U", "U", "U", 
"U", "U", "U", "Y", "Y", "Y", "Y", "Y", "D", "D", "D")), .Names = c("Seq", 
"Tpe"), row.names = c(NA, 18L), class = "data.frame")
DT <- data.table(DT, key = c("Tpe", "Seq"))
DT[,Tpe:= as.factor(Tpe)]

DT
    Seq Tpe
 1:   3   D
 2:   4   D
 3:   6   D
 4:   1   U
 5:   2   U
 6:   3   U
 7:   5   U
 8:   7   U
 9:   9   U
10:  11   U
11:  15   U
12:  23   U
13:  67   U
14:   1   Y
15:   3   Y
16:   4   Y
17:   5   Y
18:   9   Y

考虑到Tpe 中的所有级别,我可以更改Seq。但我想为Tpe 中的每个级别独立更改Seq 中的序列,忽略丢失的序列。

DT[,Seq:= as.factor(Seq)]
DT[,Seq:= interaction(DT$Seq, DT$Tpe, drop=TRUE)]
setattr(DT$Seq,"levels", seq(from = 1, to = length(levels(DT$Seq))))

DT
    Seq Tpe
 1:   1   D
 2:   2   D
 3:   3   D
 4:   4   U
 5:   5   U
 6:   6   U
 7:   7   U
 8:   8   U
 9:   9   U
10:  10   U
11:  11   U
12:  12   U
13:  13   U
14:  14   Y
15:  15   Y
16:  16   Y
17:  17   Y
18:  18   Y

想要的输出如下。

out <- structure(list(Seq = c(1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
1, 2, 3, 4, 5), Tpe = c("D", "D", "D", "U", "U", "U", "U", "U", 
"U", "U", "U", "U", "U", "Y", "Y", "Y", "Y", "Y")), .Names = c("Seq", 
"Tpe"), row.names = c(NA, 18L), class = "data.frame")

out
   Seq Tpe
1    1   D
2    2   D
3    3   D
4    1   U
5    2   U
6    3   U
7    4   U
8    5   U
9    6   U
10   7   U
11   8   U
12   9   U
13  10   U
14   1   Y
15   2   Y
16   3   Y
17   4   Y
18   5   Y

【问题讨论】:

    标签: r data.table r-factor


    【解决方案1】:

    你可以试试

    DT[, Seq1:=1:.N , by=Tpe]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 2019-06-17
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2017-12-03
      相关资源
      最近更新 更多