我有一个长的帧文件如下所示:R:最佳匹配比较
df <- structure(list(Date =c("2011-01", "2011-08", "2012-03", "2011-01", "2011-08", "2011-01", "2011-08", "2011-01", "2011-08",
"2011-01", "2011-08", "2012-03", "2011-01", "2011-08", "2011-01", "2011-08", "2011-01", "2011-08",
"2011-01", "2011-08", "2012-03", "2011-01", "2011-08", "2011-01", "2011-08", "2011-01", "2011-08"),
Part=c("A", "A", "A", "A", "A", "A", "A", "A", "A",
"B", "B", "B", "B", "B", "B", "B", "B", "B",
"C", "C", "C", "C", "C", "C", "C", "C", "C"),
method=c("Type1","Type1","Type1","Type2","Type2","Type3","Type3","Type4","Type4",
"Type1","Type1","Type1","Type2","Type2","Type3","Type3","Type4","Type4",
"Type1","Type1","Type1","Type2","Type2","Type3","Type3","Type4","Type4"),
value= c(4L, 46L, 43L, 9L, 8L, 46L, 63L, 84L, 2L, 5L, 78L, 2L, 89L, 2L, 6L, 62L, 25L, 46L, 3L, 4L, 7L, 24L, 13L, 21L, 19L, 8L, 3L)),
class= "data.frame", row.names=c(NA, -27L))
我想创建称为BestMethod
另一列。该变量应该是与部件和日期最接近类型3的值相对应的方法列表。
例如,在2011-01部分A中,应用了类型1,2,3,类型1与类型3最接近。在BestMethod
下,我将拥有Type1。否则,如果所有3种类型都没有应用,我会把NA。
(在Excel中,可能是这样的:
=INDEX(C2:F2, MATCH(MIN(ABS(C2:F2-B2)), ABS(C2:F2-B2),0))
那么这样的:
=IF(B2="", "NA", INDEX($C$1:$F$1,1,(MATCH(H2,C2:F2,0)))))
然后我想创建一个名为FinalMethod
另一列,我想有大部分上市类型。对于每个部分要复制的所有日期
例如:在2011-01,2011-02的A部分,类型1是更好的匹配,但在2011-03类型2是更好的ma TCH。在这种情况下,我希望类型1为本部分所有日期的FinalMethod
。
我试过如下:
which(abs(x-your.number)==min(abs(x-your.number)))
,但我万万麻烦调用正确的数据值,并通过各行运行它。
谢谢。
所需的输出:采用dplyr
+ tidyr
df <- structure(list(Date =c("2011-01", "2011-08", "2012-03", "2011-01", "2011-08", "2011-01", "2011-08", "2011-01", "2011-08",
"2011-01", "2011-08", "2012-03", "2011-01", "2011-08", "2011-01", "2011-08", "2011-01", "2011-08",
"2011-01", "2011-08", "2012-03", "2011-01", "2011-08", "2011-01", "2011-08", "2011-01", "2011-08"),
Part=c("A", "A", "A", "A", "A", "A", "A", "A", "A",
"B", "B", "B", "B", "B", "B", "B", "B", "B",
"C", "C", "C", "C", "C", "C", "C", "C", "C"),
method=c("Type1","Type1","Type1","Type2","Type2","Type3","Type3","Type4","Type4",
"Type1","Type1","Type1","Type2","Type2","Type3","Type3","Type4","Type4",
"Type1","Type1","Type1","Type2","Type2","Type3","Type3","Type4","Type4"),
value= c(4L, 46L, 43L, 9L, 8L, 46L, 63L, 84L, 2L, 5L, 78L, 2L, 89L, 2L, 6L, 62L, 25L, 46L, 3L, 4L, 7L, 24L, 13L, 21L, 19L, 8L, 3L),
BestModel=c("Type2", "Type1", "NA", "Type2", "Type1", "Type2", "Type1", "Type2", "Type1",
"Type1", "Type1Type4", "NA", "Type1", "Type1Type4", "Type1", "Type1Type4","Type1", "Type1Type4",
"Type2", "Type2", "NA", "Type2", "Type2", "Type2", "Type2", "Type2", "Type2"),
FinalModel= c("Type1Type2", "Type1Type2","Type1Type2", "Type1Type2","Type1Type2", "Type1Type2","Type1Type2","Type1Type2","Type1Type2",
"Type1", "Type1", "Type1", "Type1", "Type1", "Type1","Type1", "Type1", "Type1",
"Type2", "Type2","Type2", "Type2", "Type2", "Type2","Type2", "Type2", "Type2")),
class= "data.frame", row.names=c(NA, -27L))
的问题是不清楚我。 “最接近3型”是什么意思?类型3仅适用于日期:2013-08和2013-09的A,B和C,而其他两种类型不适用。在该示例中,只有类型1在2011-01日期存在。你能否让这个例子更清楚一些? – missuse
嗨!谢谢你注意到这一点。我改变了日期,所以有重叠。对于没有Type3的人,我想默认为NA。示例:对于部件A,如果类型1与2011-01-01类型3最接近,则在BestMethod列下打印类型1。如果没有类型3,则在BestMethod列下打印NA。第二部分:对于部分A,如果所有日期的总类型1的数量大于总类型2,则在FinalMethod下打印类型1。谢谢。 – flightless13wings
你能添加所需的输出吗? – Marcelo