逐列将多个函数应用于两个数据帧

时间:2016-04-26 04:07:43

标签: r dataframe mapply

给出以下样本数据:

library(Metrics)

obs=data.frame(replicate(10,runif(100)))
pred=data.frame(replicate(10,runif(100)))

obs1=as.data.frame(lapply(obs, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))
pred1=as.data.frame(lapply(pred, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))

pred1[,1]=NA 

result=mapply(function(x, y) {if(all(is.na(y))) NA else mae(x, y, ), mse(x,y),rmse(x,y),se(x,y)
}, obs1,pred1,SIMPLIFY = F,USE.NAMES = TRUE)

我想通过mae(obs1[,1],pred1[,1])计算说mapply等。如何使用base R functionsplyr通过一次通话为所有其他功能执行相同操作?

在输出中,result的rownames是column namesobs1的{​​{1}},而同名是[{1}}等。

1 个答案:

答案 0 :(得分:1)

您必须编写自己的函数来指定要应用的各种函数:

 multi.fun <- function(x,y) {
   c(mae = mae(x,y), mse = mse(x,y))
 }

然后你可以这样做:

obs=data.frame(replicate(10,runif(100)))
pred=data.frame(replicate(10,runif(100)))

obs1=as.data.frame(lapply(obs, function(cc) cc[ sample(c(TRUE, NA), prob =     c(0.85, 0.15), size = length(cc), replace = TRUE) ]))
pred1=as.data.frame(lapply(pred, function(cc) cc[ sample(c(TRUE, NA), prob =  c(0.85, 0.15), size = length(cc), replace = TRUE) ]))

mapply(multi.fun, obs1, pred1)