在R Dataframe 中查找某些值是否具有与其关联的某些其他参数

wvt8vs2t  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(115)

我希望找出某些值是否与每个类别相关联。我有一个向量x〈- c(“Jay Ambiga”,“Ameen Agent”)。对于x中的每个值,我想知道他们是否有咖啡店和餐馆与之相关联。我在描述中使用grepl来找出它是单词咖啡还是餐馆,但我不确定如何得到最终结果,看起来像这样,

df<-structure(list(Date = c("Ledger:", "44718", "44737", "44768", 
                                    "44768", "Ledger:", "44564", "44564", "44567", "44567", "44662", 
                                    "44687", "44743", "44743", "44758", "44758"), Particulars = c("Ameen Agent", 
                                                                                                  "To", "To", "To", "By", "Jay Ambiga", "To", "By", "To", "By", 
                                                                                                  "To", "To", "By", "By", "To", "By"), Description = c("1-Jan-22 to 12-Oct-22", 
                                                                                                                                                       "UOB SGD A/C - Restaurant", "UOB SGD A/C - Restaurant", 
                                                                                                                                                       "UOB SGD A/C - Restaurant", "Agent Fee", "1-Jan-22 to 12-Oct-22", 
                                                                                                                                                       "UOB SGD A/C - Coffee Shop", "Purchase of Materials - Others", 
                                                                                                                                                       "UOB SGD A/C - Coffee Shop", "Purchase of Materials - Liquor & Cigarette", 
                                                                                                                                                       "UOB SGD A/C - Restaurant", "UOB SGD A/C - Restaurant", 
                                                                                                                                                       "Purchase of Materials - Others", "Purchase of Materials - Others", 
                                                                                                                                                       "UOB SGD A/C - Restaurant", "Purchase of Materials - Groceries"
                                                                                                  ), Vch_Type = c(NA, "Payment", "Payment", "Payment", "Journal", 
                                                                                                                  NA, "Payment", "Purchase", "Payment", "Purchase", "Payment", 
                                                                                                                  "Payment", "Purchase", "Purchase", "Payment", "Purchase"), Vch_No = c(NA, 
                                                                                                                                                                                        "1150", "1255", "1415", "39", NA, "13", "4671", "62", "9", "879", 
                                                                                                                                                                                        "992", "11042022", "06052022", "1361", "16072022"), Debit = c(NA, 
                                                                                                                                                                                                                                                      "50", "300", "1000", NA, NA, "3447.5", NA, "258", NA, "293.39999999999998", 
                                                                                                                                                                                                                                                      "880", NA, NA, "20", NA), Credit = c(NA, NA, NA, NA, "1000", 
                                                                                                                                                                                                                                                                                           NA, NA, "3447.5", NA, "258", NA, NA, "293.39999999999998", "880", 
                                                                                                                                                                                                                                                                                           NA, "20"), Category = c("Ameen Agent", "Ameen Agent", "Ameen Agent", 
                                                                                                                                                                                                                                                                                                                   "Ameen Agent", "Ameen Agent", "Jay Ambiga", "Jay Ambiga", "Jay Ambiga", 
                                                                                                                                                                                                                                                                                                                   "Jay Ambiga", "Jay Ambiga", "Jay Ambiga", "Jay Ambiga", "Jay Ambiga", 
                                                                                                                                                                                                                                                                                                                   "Jay Ambiga", "Jay Ambiga", "Jay Ambiga")), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                             -16L), class = c("tbl_df", "tbl", "data.frame"))
v1l68za4

v1l68za41#

library(data.table)
setDT(df)
ans <- df[, .(temp = paste0(Description, collapse = ";")), by = .(Category)]
ans[, has_both := as.logical(
        sapply(temp, function(x) grepl("restaurant", x, ignore.case = TRUE)) * 
        sapply(temp, function(x) grepl("coffee shop", x, ignore.case = TRUE)))][, temp := NULL][]
#       Category has_both
# 1: Ameen Agent    FALSE
# 2:  Jay Ambiga     TRUE

相关问题