3 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊
以下是一些匯總嘗試:
aggregate(. ~ ID, data=dat, FUN=na.omit, na.action="na.pass")
# ID Col1 Col2 Col3 Col4
#1 1 5 10 15 20
#2 2 25 30 35 40
由于aggregate默認(rèn)情況下,公式接口na.omit會(huì)在進(jìn)行任何分組之前使用整個(gè)數(shù)據(jù),因此它將刪除其中的每一行,dat因?yàn)樗鼈兌贾辽侔粋€(gè)NA值。試試看:nrow(na.omit(dat))return 0。因此,在這種情況下,請(qǐng)使用na.passin aggregate,然后na.omit跳過NA通過的。
或者,不要使用公式接口并指定要手動(dòng)匯總的列:
aggregate(dat[-1], dat[1], FUN=na.omit )
aggregate(dat[c("Col1","Col2","Col3","Col4")], dat["ID"], FUN=na.omit)
# ID Col1 Col2 Col3 Col4
#1 1 5 10 15 20
#2 2 25 30 35 40
- 3 回答
- 0 關(guān)注
- 550 瀏覽
添加回答
舉報(bào)