R 그룹별 평균 구하기

1 개요[ | ]

R 그룹별 평균 구하기
  • 대략 "SELECT fruit, AVERAGE(ea) FROM df GROUP BY fruit"
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

2 방법 1: aggregate ★[ | ]

aggregate(ea ~ fruit, df, mean)

3 방법 2: tapply[ | ]

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

4 방법 3: sqldf[ | ]

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

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}