作者paranoia5201 (社會黑暗、行路難)
看板R_Language
標題[問題] all.equal()不用印出所有資訊
時間Sat Apr 27 11:26:13 2019
大家好:
這裡想要詢問迴圈資料「選擇性輸出」的做法。
有一份資料,我要比對它的欄位兩兩是否相同。
【單筆測試】
> 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"
感謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.194.164.43
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1556335575.A.92D.html
→ andrew43: 不明白為什麼只有第一次的比較被你視為不一致 04/27 22:15
推 TOOYA: print(c(i,match[2])) ? 04/30 14:57
→ TOOYA: print(i);print(match[2]) ? 04/30 14:58
→ paranoia5201: 原來可以這樣選!感謝樓上賜教。 04/30 22:53