在R data.table

时间:2018-07-06 19:58:50

标签: r data.table

这个问题类似于How to change type of target column when doing := by group in a data.table in R?,但是,我需要使用.()表示法。

示例:

a <- data.table(ID = c("A", "B", "C", "A", "B", "C"), 
                TYPE = c(1, 1, 2, 2, 3, 3), 
                CLASS = c(1, 2, 3, 4, 5, 6))

b <- a[, .(Count = .N, "Failure Count" = sum(CLASS == "2"), 
       "Median DIF" = median(TYPE)), by = ID]

是否可以使用这种不同的符号来做到这一点?原因是我想创建一个新表,而不是使用:=表示法向现有表中添加一列。

1 个答案:

答案 0 :(得分:0)

显然,您要做的就是在as.numeric()内添加median()

b <- a[, .(Count = .N, "Failure Count" = sum(CLASS == "2"), 
      "Median DIF" = median(as.numeric(TYPE))), by = ID]