[問題] all.equal()不用印出所有資訊

作者: paranoia5201 (邁向研究生)   2019-04-27 11:26:13
大家好:
這裡想要詢問迴圈資料「選擇性輸出」的做法。
有一份資料,我要比對它的欄位兩兩是否相同。
【單筆測試】
> m <- all.equal(list.d2[1, ], list.d2[2, ])
> m
[1] "Attributes: < Component “row.names”: Mean relative difference: 1 >"
[2] "Component “phone.4”: 1 string mismatch"
由於筆數繁多,所以我寫了一個迴圈來重複執行:
> for(i in seq(1, 5, by = 2)){
+ match <- all.equal(list.d2[i, ], list.d2[i+1, ])
+ print(i)
+ print(match)
+ }
[1] 1
[1] "Attributes: < Component “row.names”: Mean relative difference: 1 >"
[2] "Component “phone.4”: 1 string mismatch"
[1] 3
[1] "Attributes: < Component “row.names”: Mean relative difference:
0.3333333 >"
[1] 5
[1] "Attributes: < Component “row.names”: Mean relative difference: 0.2 >"
這裡是想請問:我要如何讓這迴圈只列印有不一致的資訊,而其他不用顯示?
【理想結果】
[1] 1
[2] "Component “phone.4”: 1 string mismatch"
感謝~
作者: paranoia5201 (邁向研究生)   2019-04-30 22:53:00
原來可以這樣選!感謝樓上賜教。
作者: TOOYA (在草地等流星)   2019-04-30 14:57:00
print(c(i,match[2])) ?print(i);print(match[2]) ?
作者: andrew43 (討厭有好心推文後刪文者)   2019-04-27 22:15:00
不明白為什麼只有第一次的比較被你視為不一致

Links booklink

Contact Us: admin [ a t ] ucptt.com