※ 引述《chuubbyy (byetos)》之銘言:
: ※ 引述《piggood (成為有品味的男人)》之銘言:
: 我都比較建議用data.table的方式,簡潔又快。
: library(data.table)
: setDT(medical)
: medical <- medical[,docvis.sum:=sum(docvis), by = "id"][,hhninc.mean:=mean(hhninc), by =
: "id"][, .SD[.N], by = "id"]
: 上面是data.table特殊chaining方式,若要考慮易讀性可以分3行。
: medical[,docvis.sum:=sum(docvis), by = "id"]
: medical[,hhninc.mean:=mean(hhninc), by = "id"]
: medical <- medical[, .SD[.N], by = "id"]
: 或是pipe的方式。
: medical <- medical[,docvis.sum:=sum(docvis), by = "id"] %>%
: .[,hhninc.mean:=mean(hhninc), by = "id"] %>%
: .[, .SD[.N], by = "id"]
data.table 真的也不錯用,功能強大速度也快,程式也精簡
只是對我這個新手來說,還要再多多練習...
看懂 c 大的 code 之後,練習另一種寫法,也可以達到原 po 的需求 ^^
medical <- medical[,':='(docvis.sum = sum(docvis), hhninc.mean = mean(hhninc)),
by ="id"][,.SD[.N], by = "id"]
如果本來的 hhninc 和 docvis 也要拿掉的話,後面再多加上一段
medical <- medical[,':='(docvis.sum = sum(docvis), hhninc.mean = mean(hhninc)),
by ="id"][,.SD[.N], by = "id"][,c("docvis","hhninc"):= NULL]