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

看板R_Language作者 (社會黑暗、行路難)時間6年前 (2019/04/27 11:26), 編輯推噓1(103)
留言4則, 3人參與, 6年前最新討論串1/1
大家好: 這裡想要詢問迴圈資料「選擇性輸出」的做法。 有一份資料,我要比對它的欄位兩兩是否相同。 【單筆測試】 > 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

04/27 22:15, 6年前 , 1F
不明白為什麼只有第一次的比較被你視為不一致
04/27 22:15, 1F

04/30 14:57, 6年前 , 2F
print(c(i,match[2])) ?
04/30 14:57, 2F

04/30 14:58, 6年前 , 3F
print(i);print(match[2]) ?
04/30 14:58, 3F

04/30 22:53, 6年前 , 4F
原來可以這樣選!感謝樓上賜教。
04/30 22:53, 4F
文章代碼(AID): #1SmylNaj (R_Language)
文章代碼(AID): #1SmylNaj (R_Language)