※ 引述《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]