1
我喜歡數據表並使用它來有條件地重命名或添加因子。但是,我似乎無法一次完成多個因素。這裏有一個例子:與數據表同時重命名多個因素
a <- rep(c("A", "B", "C", "D"), each=3)
b <- 1:12
df <- data.frame(a,b)
DT <- data.table(df)
現在加入新列「新」,這對所有「A」 S在列「一」等於「Z」
DT[a=="A", New:="z"]
這工作得很好。現在,如果我想改變說「A」和「C」等於「Z」:
DT[a==c("A", "C"), New:="z"]
讓我好笑的答案:
dput(DT)
structure(list(a = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 4L, 4L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"),
b = 1:12, New = c("z", NA, "z", NA, NA, NA, NA, "z", NA,
NA, NA, NA)), .Names = c("a", "b", "New"), row.names = c(NA,
-12L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000000140788>, index = structure(integer(0), a = integer(0)))
我敢肯定,這是簡單的東西,我似乎無法在SO上找到它(排隊!)。由於
爲了確認,我所期望的輸出是:
dput(DT)
structure(list(a = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 4L, 4L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"),
b = 1:12, New = c("z", "z", "z", NA, NA, NA, "z", "z", "z",
NA, NA, NA)), .Names = c("a", "b", "New"), row.names = c(NA,
-12L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000000140788>, index = structure(integer(0), a = integer(0)))