R 그룹별 평균 구하기

1 개요[ | ]

R 그룹별 평균 구하기
  • 대략 "SELECT fruit, AVERAGE(ea) FROM df GROUP BY fruit"
R
Reload
Copy
df <- read.csv( header=T, stringsAsFactors=F, text="
day,fruit,ea
2019-06-01,apple,1
2019-06-11,apple,1
2019-06-21,banana,2
2019-07-01,apple,3
2019-07-11,banana,4
2019-08-25,orange,2
")
df
Loading

2 방법 1: aggregate ★[ | ]

Copy
aggregate(ea ~ fruit, df, mean)
Loading

3 방법 2: tapply[ | ]

Copy
tapply(df$ea, df$fruit, mean)
Loading

4 방법 3: sqldf[ | ]

Copy
library(sqldf)
sqldf("SELECT fruit, 1.0*SUM(ea)/COUNT(ea) AS mean FROM df GROUP BY fruit")
Loading

5 같이 보기[ | ]