R-替换选择行数据帧
我有一个数据框“a”,形状如下R-替换选择行数据帧,r,replace,frame,R,Replace,Frame,我有一个数据框“a”,形状如下 log P1 P2 P3 P4 P5 Method Round #TSamples #Samples0 #Samples1 FP TN TP FN Time Det_Time data1 2 0 0 0 LOF 1 3 3 0 0 3 0 0 0.00800108909606934 1 data1 2 0 0 0 Mahalanobis 1
log P1 P2 P3 P4 P5 Method Round #TSamples #Samples0 #Samples1 FP TN TP FN Time Det_Time
data1 2 0 0 0 LOF 1 3 3 0 0 3 0 0 0.00800108909606934 1
data1 2 0 0 0 Mahalanobis 1 3 3 0 0 3 0 0 0.00100016593933105 1
data1 2 0 0 0 Cook 1 3 3 0 0 3 0 0 0.00900101661682129 1
...........
和另一个数据帧“B”,带有
基本上,当A和B数据帧中的列“P1”、“P2”、“P3”、“P4”、“P5”、“Method”和“Round”匹配时,我希望将数据帧A中的行“FP”、“TN”、“TP”和“FN”替换为数据帧B中的行
劳尔像这样:
require(sqldf)
C <- sqldf('select A.log, A.P1, A.P2, A.P3, A.P4, A.P5, A.Method, A.Round, "A.#TSamples", "A.#Samples0", "A.#Samples1", B.FP, B.TN, B.TP, B.FN, A.Time, A.Det_Time
from A
inner join B on (A.log = B.log and
A.P1 = B.P1 and
A.P2 = B.P2 and
A.P3 = B.P3 and
A.P4 = B.P4 and
A.P5 = B.P5 and and
A.Method = B.Method and
A.Round = B.Round)
')
C <- rbind(A,C)
C <- C[!duplicated(C[c("log", "P1", "P2", "P3", "P4", "P5", "Method", "Round", "#TSamples", "#Samples0", "#Samples1", "Time", "Det_Time")]),]
require(sqldf)
我已经尝试过了,但是它添加了额外的列。是的,它会为您需要的列子集。数据帧A有7073,数据帧B 885,它应该生成7073的数据帧,但它只生成885。我怎样才能修好它?这取决于合并函数中的顺序?merge(A,B,…)或merge(B,A,…)merge(A,B,all.x=TRUE)
,您还需要指定by=
参数。确实如此。但是,来自FP、TN、TP和FN列的其余数据将插入NA。为什么?@Ruser-我编辑了我的答案现在可以使用了吗?在sqliteSendQuery(con,statement,bind.data)中出现以下错误:语句中的错误:不明确的列名:FP@Ruser-我的错误,我会尽快改进。如果你需要更多信息,请告诉我。非常感谢。
require(sqldf)
C <- sqldf('select A.log, A.P1, A.P2, A.P3, A.P4, A.P5, A.Method, A.Round, "A.#TSamples", "A.#Samples0", "A.#Samples1", B.FP, B.TN, B.TP, B.FN, A.Time, A.Det_Time
from A
inner join B on (A.log = B.log and
A.P1 = B.P1 and
A.P2 = B.P2 and
A.P3 = B.P3 and
A.P4 = B.P4 and
A.P5 = B.P5 and and
A.Method = B.Method and
A.Round = B.Round)
')
C <- rbind(A,C)
C <- C[!duplicated(C[c("log", "P1", "P2", "P3", "P4", "P5", "Method", "Round", "#TSamples", "#Samples0", "#Samples1", "Time", "Det_Time")]),]