0
我試圖創建一個函數,它將爲數據集中選定的一組列創建新的標記指示符列。創建循環遍歷2個向量的列
# Data Set
A = as.factor(c(0,2,1,0))
B = as.factor(c(2,NA,1,0))
C = as.factor(c(1,0,NA,0))
D = as.factor(c(NA,2,0,1))
dat = data.table(A, B, C, D)
目前我做這個,我想每一列:
# What I'm currently doing (expected output of loop matches these columns)
attach(dat)
VAR = B
dat$b.test[VAR == "0"] <- "0"
dat$b.test[VAR == "1" | VAR == "2"] <- "1"
VAR = C
dat$c.test[VAR == "0"] <- "0"
dat$c.test[VAR == "1" | VAR == "2"] <- "1"
VAR = D
dat$d.test[VAR == "0"] <- "0"
dat$d.test[VAR == "1" | VAR == "2"] <- "1"
detach(dat)
好像我應該能夠創建一個for循環,將運行一個包含所有列向量我希望邏輯在(B,C,D)上執行,並從每個向量中相同位置的另一個向量(b2,c2,d2)調用一個新名稱。
嘗試方法1個
# Failed method 1
attach(dat)
new.var = c(b2, c2, d2)
cur.var = c(B, C, D)
l = length(cur.var)
for(i in 1:l){
X = cur.var[i]
VAR = cur.var[i]
dat$X[VAR == "0"] <- "0"
dat$X[VAR == "1" | VAR == "2"] <- "1"
}
detach(dat)
結果在新的列X
嘗試方法2
# Failed method 2
new.var = c(dat$b2, dat$c2, dat$d2)
cur.var = c(dat$B, dat$C, dat$D)
l = length(cur.var)
for(i in 1:l){
new.var[i] = ifelse(new.var[i] == "0", "0",
ifelse(new.var[i] == "1" | "2", "1", NA)
)
}
是否有不同的方法,試圖做到這一點?
歡迎來到SO。請給我們提供您想要的輸出的df。 –