必須是避免在R中執行此操作的一種方法。某種映射函數?基本上,要指定一系列grepl條件和一系列替換。謝謝,R映射等效?
hold[grepl('TRAVEL', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "TRAVEL"
hold[grepl('IN-KIND:', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "IN-KIND"
hold[grepl('COMPLIANCE', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "COMPLIANCE"
hold[grepl('MEDIA', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "MEDIA"
hold[grepl('WATER|FOOD|OFFICE|Office|CLEANING|ALARM', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "OFFICE"
hold[grepl('DATA', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "DATA"
hold[grepl('LEGAL', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "LEGAL"
hold[grepl('EVENT', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "EVENT"
hold[grepl('CATERING', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "CATERING"
hold[grepl('PHONE', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "PHONE"
hold[grepl('EQUIPMENT', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "EQUIPMENT"
hold[grepl('FUNDRAISING|FINANCE', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "FUNDRAISING"
hold[grepl('PAYROLL', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "PAYROLL"
hold[grepl('LIST RENTAL', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "LIST RENTAL"
編輯:是啊,對不起,有點輕浮。 hold是data.frame,其中一列名爲「exp_pur_desc」。我可以看到lapply或sapply與「開關」等效。謝謝。
我猜你想要'do.call'和/或'lapply',但是你的代碼不可重現,所以它仍然是一個猜測,因爲我們不知道什麼是持有。 – 2012-04-11 20:11:16
爲什麼不建立玩具來源和目標數據集只有幾個這樣的列。那麼你應該說什麼是正確的答案。 – 2012-04-11 20:35:46
看起來你正在用'TRAVEL'等替換'TRAVEL'等許多似乎多餘的行 - 或者我錯過了什麼? – Tommy 2012-04-11 21:16:17