我試圖用一組簡單的代碼替換多個單獨的Mapply語句。我終於得到它與3嵌套mapply聲明,但似乎有點複雜的方法。我是來自其他語言的新手,所以在R心態尋找一些幫助來思考。如果這三個陳述是最好的方法,我可以接受它,但要尋找輸入。如果你有更好的方法來構造像這樣的子集化輸出,那麼我就是耳朵。簡化嵌套Mapply語句
payments <- data.frame(
Amount = sample(5:15,100,replace=TRUE),
Tip.Amount = round(runif(100,0,2),2),
"A" = sample(c(TRUE,FALSE),100,replace=TRUE),
"B" = sample(c(TRUE,FALSE),100,replace=TRUE),
"C" = sample(c(TRUE,FALSE),100,replace=TRUE),
"D" = sample(c(TRUE,FALSE),100,replace=TRUE),
"E" = sample(c(TRUE,FALSE),100,replace=TRUE),
"F" = sample(c(TRUE,FALSE),100,replace=TRUE),
Date = sample(seq(as.Date("2016-01-01"),as.Date("2016-01-31"),by="day"),100,replace=TRUE)
)
employees <- c("A","B","C","D","E","F")
dots <- lapply(c(employees,"Date"),as.symbol)
payments.by_date_employee <- payments %>%
filter(!is.na(Date),!is.na(Amount)) %>%
group_by_(.dots=dots) %>%
summarise(Payment.Count=n(), Amount=sum(Amount),
Tip.Count=sum(Tip.Amount>=0.01,na.rm=TRUE), Tip.Amount=sum(Tip.Amount,na.rm=TRUE)) %>%
ungroup() %>%
arrange(Date)
#long/manual way--------------------------------------------------------------------------------
t <- list()
t[["payments"]][["amount"]] <- mapply(function(name) list({
t.test(subset(payments,payments[[name]]==TRUE)$Amount,
subset(payments,payments[[name]]==FALSE)$Amount)$p.value
}),
employees)
t[["payments"]][["count"]] <- mapply(function(name) list({
t.test(subset(payments.by_date_employee,payments.by_date_employee[[name]]==TRUE)$Amount,
subset(payments.by_date_employee,payments.by_date_employee[[name]]==FALSE)$Amount)$p.value
}),
employees)
t[["tips"]][["amount"]] <- mapply(function(name) list({
t.test(subset(payments,payments[[name]]==TRUE)$Tip.Amount,
subset(payments,payments[[name]]==FALSE)$Tip.Amount)$p.value
}),
employees)
t[["tips"]][["count"]] <- mapply(function(name) list({
t.test(subset(payments.by_date_employee,payments.by_date_employee[[name]]==TRUE)$Tip.Amount,
subset(payments.by_date_employee,payments.by_date_employee[[name]]==FALSE)$Tip.Amount)$p.value
}),
employees)
#long/manual way--------------------------------------------------------------------------------
#attempt at single mapply statement ------------------------------------------------------------
y <- mapply(function(name,type,variable,df,nm) list({
t.test(subset(eval(df),eval(df)[[name]]==TRUE)[[nm]],
subset(eval(df),eval(df)[[name]]==FALSE)[[nm]])$p.value}),
employees,
c("payments","payments","tips","tips"),
c("amount","count"),
c(quote(payments),quote(payments),quote(payments.by_date_employee),quote(payments.by_date_employee)),
c("Amount","Amount","Tip.Amount","Tip.Amount"),
SIMPLIFY = FALSE
)
#attempt at single mapply statement ------------------------------------------------------------
#works but seems convoluted --------------------------------------------------------------------
z <- mapply(function(type) list({
mapply(function(variable,df,nm) list({
t[[type]][[variable]] <-mapply(function(name) list({
t.test(subset(eval(df),eval(df)[[name]]==TRUE)[[nm]],
subset(eval(df),eval(df)[[name]]==FALSE)[[nm]])$p.value}),
employees)
}),
c("amount","count"),
c(quote(payments),quote(payments),quote(payments.by_date_employee),quote(payments.by_date_employee)),
c("Amount","Amount","Tip.Amount","Tip.Amount"),
SIMPLIFY = FALSE
)
}),
c("payments","tips")
)
#works but seems convoluted --------------------------------------------------------------------
絕對看到它的工作原理。在標記爲正確之前嘗試理解你所做的事情。你正在做一些對我來說很陌生的事情! – atclaus
您會如何建議從t檢驗中提取額外的值?我正在尋找x和y的意思,所以我可以總結出存在差異的方向...... – atclaus
請參閱編輯。 –