2015-11-19 76 views
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))) 

回答

5

您應該使用%in%代替==,因此你將需要:

DT[a %in% c("A", "C"), New:="z"] 

這給:

> DT 
    a b New 
1: A 1 z 
2: A 2 z 
3: A 3 z 
4: B 4 NA 
5: B 5 NA 
6: B 6 NA 
7: C 7 z 
8: C 8 z 
9: C 9 z 
10: D 10 NA 
11: D 11 NA 
12: D 12 NA 

點使用的數據:

a <- rep(c("A", "B", "C", "D"), each=3) 
b <- 1:12 
DT <- data.table(a,b) 

在數據框,你可以這樣做:

df <- data.frame(a,b) 
df$New <- NA 
df[df$a %in% c("A", "C"), "New"] <- "z" 

來達到同樣的效果。