将mapply函数的输出强制转换为数据帧R.

时间:2016-04-22 20:52:20

标签: r dataframe rbind mapply coerce

library(verification)

考虑到几个实验(AA,BB,...,),我可以使用此reproducible example按如下方式运行我的代码:

##################### EXPERIMENT AA

#my data in reality is continuous but I convert it to binary using a threshold 

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

#introduce a few NA values to simulate real world data.
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) ]))

#A=mapply(verify,obs1,pred1,frcst.type = "binary", obs.type = "binary")
pred1[,1]=NA # some points in pred1 have all NAs in reality

a=mapply(function(x, y) {if(all(is.na(y))) NA else verify(x, y, frcst.type = "binary", obs.type = "binary")
}, obs1,pred1,SIMPLIFY = F,USE.NAMES = TRUE)

aa=do.call('rbind',a)# put all paramters together and keep rownames for easy identification

##################### EXPERIMENT BB

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

#introduce a few NA values to simulate real world data.
obs2=as.data.frame(lapply(obs, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))
pred2=as.data.frame(lapply(pred, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))

#A=mapply(verify,obs1,pred1,frcst.type = "binary", obs.type = "binary")
pred2[,1]=NA # some points in pred1 have all NAs in reality

b=mapply(function(x, y) {if(all(is.na(y))) NA else verify(x, y, frcst.type = "binary", obs.type = "binary")
}, obs2,pred2,SIMPLIFY = F,USE.NAMES = TRUE)

bb=do.call('rbind',b)# put all paramters together and keep rownames for easy identification

1)我想强制aabb强制称为Answer的数据框忽略$tab, $pred,$obs。请注意,rownamesaa中的bb是输入数据的colnames(所有输入都具有相同的名称)

2)对于AA中的实验BB...Answer,我希望两个实验中的每个参数都有相邻的列,例如TS中的AnswerTS1Ts2分别代表实验AA和BB。

0 个答案:

没有答案