大家好:
這裡想要詢問迴圈資料「選擇性輸出」的做法。
有一份資料,我要比對它的欄位兩兩是否相同。
【單筆測試】
> 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"
感謝~