2016-03-06 29 views
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 

任何建議大大(!)的讚賞。

+2

「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] ' –

+0

也許'DT [,(cList):= lapply(.SD,function(x)replace(x,x> 1,1)),.SDcols = cList]'可能會快一點, 'DT []'類型代碼。然而,@DavidArenburg的第一批data.table代碼對於速度來說可能是理想的選擇。 – thelatemail

+0

我與你所建議的,涉及集(DT ...)去。像魅力一樣工作。我曾嘗試過使用set,但沒有考慮使用它。我該如何給這個答案複選標記? – JerryN

回答

0

正如David在評論中所說的那樣,您會收到警告,因爲如果在評估條件時只採用第一個元素。因此,您可以使用矢量化的ifelse。

這是我使用pmin的替代解決方案。

DT <-as.data.table(matrix(1:9, 3,3)) 
cList <- c("V1","V2") 

DT[, (cList) := lapply(.SD, function(x) pmin(1, x)), .SDcols=cList] 

希望這會有所幫助。