必须是一种避免在 R 中这样做的方法。某种映射函数?基本上,想要指定一系列的grepl条件和一系列的替换。谢谢,
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 使用“switch”等效项。谢谢。