[R] 연습

[R] aggregate() 함수

Simon Yoon 2022. 4. 29. 22:39

aggregate() 함수는 데이터의 통계량을 계산해주는 함수이다.

  • Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form.

 

  • 함수의 기본 형태
# 'data.frame'
aggregate(data, by, FUN, ...)

# 'formula'
aggregate(formula, data, FUN, ...)
by a list of grouping elements, each as long as the variable in the data frame x.
The elements are coerced to factors before use.
FUN a function to compute the summary statistics which can be applied to all data subsets.
formula a formula, such as y ~ x or cbind(y1, y2) ~ x1 + x2, where the y variables are numeric data to be split into groups according to the grouping x variables.

 

  • 함수 활용 예시
# 'data.frame'
aggregate(x = testDF, by = list(fby1, fby2), FUN = "mean")

# 'formula'
aggregate(weight ~ feed, data = chickwts, mean)  # one ~ one
aggregate(breaks ~ wool + tension, data = warpbreaks, mean)  # one ~ many
aggregate(cbind(Ozone, Temp) ~ Month, data = airquality, mean)  # many ~ many

## Dot notation
### Species를 기준으로 다른 모든 컬럼의 평균값 구하기
aggregate(. ~ Species, data = iris, mean)
### len을 제외한 다른 모든 컬럼을 기준으로 len의 평균 구하기
aggregate(len ~ ., data = ToothGrowth, mean)

'[R] 연습' 카테고리의 다른 글

[R] 문자열 합치기  (0) 2022.05.14
[R] 특정 열 삭제하기  (0) 2022.05.08
[R] sub(), gsub() 함수  (0) 2022.04.24
[R] subset() 함수  (0) 2022.04.22
[R] 데이터프레임 관련 함수 및 코드  (0) 2022.04.17