2017-04-17 49 views
0

我想根據兩個條件刪除一些行。 這是我的代碼根據r中的多個條件刪除行

test <-datasetjoin[!(datasetjoin$Occupation == "Clerical" & datasetjoin$AvgMonthSpend > 58.515),]
test <- test[!(test$Occupation == "Management" & test$AvgMonthSpend > 59.24),] test <- test[!(test$Occupation == "Manual" & test$AvgMonthSpend > 54.28),] test <- test[!(test$Occupation == "Professional" & test$AvgMonthSpend > 60.08),]
test <- test[!(test$Occupation == "Skilled Manual" & test$AvgMonthSpend > 57.06),] test <- test[!(test$NumberCarsOwned == "1" & test$YearlyIncome > (81300-51140) * 1.5 + 81300),]
是否有可能以更優雅的方式獲得相同的結果?

預先感謝

Occupation MonthlySpend 
Clerical 60   
Management 59   
Clerical 62   
Clerical 58   
Clerical 63    
Management 56 
Management 58  

如果職業=文書及MonthlySpend> 60然後丟棄這些行 如果職業=管理和MonthlySpend> 57然後丟棄這些行。 最後我應該得到這樣的:

Occupation MonthlySpend 
Clerical 58 
Management 56 
+0

請爲您的問題提供[可重現的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 –

+0

@AdamQuek請檢查帖子,我用一個例子編輯。有沒有辦法做到這一點與循環?或應用()? –

回答

2

使用或組合的所有條件:|

像:

test <- test[!(test$Occupation == "Management" & test$AvgMonthSpend > 59.24) | !(test$Occupation == "Manual" & test$AvgMonthSpend > 54.28),] 
1

,你可以嘗試這樣的事情。

步驟1.定義限制:

df <- read.table(text="Occupation MonthlySpend 
Clerical 60   
Management 59   
Clerical 62   
Clerical 58   
Clerical 63    
Management 56 
Management 58 ", stringsAsFactors=FALSE, header = TRUE) 


df2 <- read.table(text="Occupation lmt 
Clerical 60   
Management 57   
", stringsAsFactors=FALSE, header = TRUE) 

第二步。加入和過濾

df %>% left_join(df2, by = "Occupation") %>% 
    group_by(Occupation) %>% 
    filter(MonthlySpend < lmt) %>% 
    select(MonthlySpend) 

這給:

Source: local data frame [2 x 2] 
Groups: Occupation [2] 

    Occupation MonthlySpend 
     <chr>  <int> 
1 Clerical   58 
2 Management   56 

這樣一來,你要花費一些資源在確定第二數據幀,但過濾的實際過程被簡化。