2
我想,如果他們是大於1R:使用具有「如果」
我已經嘗試了許多不同的東西來設置在多個列中的值在數據表中,以1的功能通過引用更新數據表中的列。該代碼看起來像它應該工作,但給了我下面的代碼的消息,並刪除第一列(V1)
DT <-as.data.table(matrix(1:9, 3,3))
cList <- c("V1","V2")
DT[,(cList) := lapply(.SD, function(x) if (x > 1) 1),.SDcols = cList]
Warning messages:
1: In if (x > 1) 1 :
the condition has length > 1 and only the first element will be used
2: In if (x > 1) 1 :
the condition has length > 1 and only the first element will be used
任何建議大大(!)的讚賞。
「x」是一個向量,「if」接受長度爲1的表達式。你可以用'ifelse'來解決這個問題,但如果我是你,我會用'for(j in cList)set(DT,i = which(DT [[j]]> 1L),j = j,value = 1L)'或'ifelse'方式(不推薦)將會是DT [,(cList):= lapply(.SD,function(x)ifelse(x> 1L,1L,x)),.SDcols = cList] ' –
也許'DT [,(cList):= lapply(.SD,function(x)replace(x,x> 1,1)),.SDcols = cList]'可能會快一點, 'DT []'類型代碼。然而,@DavidArenburg的第一批data.table代碼對於速度來說可能是理想的選擇。 – thelatemail
我與你所建議的,涉及集(DT ...)去。像魅力一樣工作。我曾嘗試過使用set,但沒有考慮使用它。我該如何給這個答案複選標記? – JerryN