假设我有一个包含5列的R数据框,如下所示
time MeanVar1 SdVar1 MedianVar1 MeanVar2 SdVar2
1 -0.8453978 -1.636985 -0.6239832 -0.4366982 -1.7037374
2 -0.3000778 -1.034199 0.3292459 -0.6606399 -0.1525361
是否有一种简洁的方法来生成dataFrame,如下所示:
Var time Mean/Median SD
1 1 -0.8453978 -1.636985
1 2 -0.3000778 -1.034199
1 1 -0.6239832 N/A
1 2 0.3292459 N/A
2 1 -0.4366982 -1.7037374
2 2 -0.6606399 -0.1525361
或
Var time Mean/Median SD
MeanVar1 1 -0.8453978 -1.636985
MeanVar1 2 -0.3000778 -1.034199
MeanVar1 1 -0.6239832 N/A
MeanVar1 2 0.3292459 N/A
MeanVar2 1 -0.4366982 -1.7037374
MeanVar2 2 -0.6606399 -0.1525361
我的总体意图是在同一图中绘制变量1的平均值,SD与变量1,变量1的中值和平均值,变量1的SD。因此,我觉得如果我以这种格式修改数据,我可以立即绘制它而不是分别绘制每一行。
由于我对重塑和融化知识有限,我无法做到这一点。
编辑:添加更多信息
示例输入(给定3行,总共有100行):
Label trainingSize Accuracy_Mean Accuracy_SD Accuracy_SE Precision_Mean Recall_Mean F1 Accuracy_Median PriorClass0_Mean PriorClass0_SD PriorClass0_SE ProbabilityEstimate_0given0_Mean ProbabilityEstimate_0given0_SD ProbabilityEstimate_0given0_SE ProbabilityEstimate_0given1_Mean ProbabilityEstimate_0given1_SD ProbabilityEstimate_0given1_SE
0perc_0repeat 0.4 0.5506 0.0531 0.0038 0.6374 0.2336 0.3419 0.5372 0.5278 0.0254 0.0018 0.6433 0.0028 0.0 0.4169 0.003 0.0
0perc_0repeat 0.4 0.5456 0.0482 0.0034 0.6465 0.2142 0.3218 0.5333 0.5304 0.0248 0.0018 0.6414 0.0028 0.0 0.4193 0.0027 0.0
0perc_0repeat 0.4 0.5574 0.0555 0.0039 0.6604 0.2197 0.3297 0.5404 0.529 0.0233 0.0016 0.6436 0.003 0.0 0.4163 0.0029 0.0
我正在尝试绘制
1) the iteration number(1:100) in X Axis and the points of 5 columns (Accuracy_Mean, Accuracy_Median, PriorClass0_Mean, ProbabilityEstimate_0given0_Mean, ProbabilityEstimate_0given1_Mean in the Y AXIS.
2) distribution (density obtained by 100 points) of 5 columns with error bars (either SD or SE) in a single plot using ggplot.
我有4列Precision_Mean,Recall_Mean,F1,Accuracy_Median不遵循均值,sd,se模式!
EDIT1: 1)
dput(droplevels(head(data,3))) 结构(list(标签=结构(c(1L,1L,1L),. Label =" 0perc_0repeat",class =" factor"), trainingSize = c(0.4,0.4,0.4),Accuracy_Mean = c(0.5506, 0.5456,0.5574),Accuracy_SD = c(0.0531,0.0482,0.0555), Accuracy_SE = c(0.0038,0.0034,0.0039),Precision_Mean = c(0.6374, 0.6465,0.6604),Recall_Mean = c(0.2336,0.2142,0.2197), F1 = c(0.3419,0.3218,0.3297),Accuracy_Median = c(0.5372, 0.5333,0.5404),PriorClass0_Mean = c(0.5278,0.5304,0.529 ),PriorClass0_SD = c(0.0254,0.0248,0.0233),PriorClass0_SE = c(0.0018, 0.0018,0.0016),ProbabilityEstimate_0given0_Mean = c(0.6433, 0.6414,0.6436),ProbabilityEstimate_0given0_SD = c(0.0028, 0.0028,0.003),ProbabilityEstimate_0given0_SE = c(0,0, 0),ProbabilityEstimate_0given1_Mean = c(0.4169,0.4193, 0.4163),ProbabilityEstimate_0given1_SD = c(0.003,0.0027, 0.0029),ProbabilityEstimate_0given1_SE = c(0,0,0)),。Name = c(" Label", " trainingSize"," Accuracy_Mean"," Accuracy_SD"," Accuracy_SE", " Precision_Mean"," Recall_Mean"," F1"," Accuracy_Median"," PriorClass0_Mean", " PriorClass0_SD"," PriorClass0_SE"," ProbabilityEstimate_0given0_Mean", " ProbabilityEstimate_0given0_SD"," ProbabilityEstimate_0given0_SE", " ProbabilityEstimate_0given1_Mean"," ProbabilityEstimate_0given1_SD", " ProbabilityEstimate_0given1_SE"),row.names = c(NA,3L),class =" data.frame")
2)预期输出类似于:
Vars Label trainingSize Mean SD SE
Vars:Mean,PriorClass0,ProbabilityEstimate_0given0,ProbabilityEstimate_0given1; (中位数,精确度,召回率,F1不是必需的,或者它们可以适合上表,SD,SE为N / A或0)。
答案 0 :(得分:3)
merged.stack
在一定程度上处理了这个问题,但是它会从“SdVar”列中回收这些值(因此我没有得到您在“divdar”列中显示的NA
值期望的输出)。
然而,这可能是解决问题的开始,所以这就是方法:
library(splitstackshape)
merged.stack(mydf, var.stubs = c("MeanVar|MedianVar", "SdVar"), sep = "var.stubs")
# time .time_1 MeanVar|MedianVar SdVar
# 1: 1 1 -0.8453978 -1.6369850
# 2: 1 1 -0.6239832 -1.6369850
# 3: 1 2 -0.4366982 -1.7037374
# 4: 2 1 -0.3000778 -1.0341990
# 5: 2 1 0.3292459 -1.0341990
# 6: 2 2 -0.6606399 -0.1525361
如果你真的想要那些NA
值,也许这样就可以了:
merged.stack(
mydf, var.stubs = c("MeanVar|MedianVar", "SdVar"),
sep = "var.stubs")[, SdVar := ifelse(
duplicated(SdVar), NA, SdVar), by = time][]
# time .time_1 MeanVar|MedianVar SdVar
# 1: 1 1 -0.8453978 -1.6369850
# 2: 1 1 -0.6239832 NA
# 3: 1 2 -0.4366982 -1.7037374
# 4: 2 1 -0.3000778 -1.0341990
# 5: 2 1 0.3292459 NA
# 6: 2 2 -0.6606399 -0.1525361