-
[R기초] apply 계열 함수의 활용법R 통계/R 기초 2021. 9. 17. 08:00728x90
지난번 포스팅에서 split과 sapply를 통해 집단별 기술 통계 값(평균 등)을 알아본 바 있다.
이번 포스팅에서는 apply 계열(apply family)의 함수들 (apply, lapply, sapply, mapply, tapply)의 사용 문법을 알아본다.
0. apply와 유사한 기능을 하는 colMean() 과 RowMean() 함수
- colMean() : 변수의 열 방향 평균 값을 계산해줌
- colMean() : 변수의 행 방향 평균 값을 계산해줌
*합산 값을 산출해주는 colSums와 rowSums도 있다.
#임의 데이터 생성 gender <- c("남", "여", "남", "여", "남") height <- c(170, 165, 183, 155, 178) weight <- c(68.7, 57.4, 83.0, 55.2, 71.4) BMI_calc <- data.frame(gender, height, weight) colMeans(BMI_calc[,2:3], na.rm = FALSE) <결과1> height weight 170.20 67.14 rowMeans(BMI_calc[,2:3], na.rm = FALSE) <결과2> [1] 119.35 111.20 133.00 105.10 124.70
1. apply(변수, Function, 기술통계값)
- Function이 1인 경우: 행 단위의 기술통계값을 계산
- Function이 2인 경우: 열 단위의 기술통계값을 계산
#임의 데이터 생성 gender <- c("남", "여", "남", "여", "남") height <- c(170, 165, 183, 155, 178) weight <- c(68.7, 57.4, 83.0, 55.2, 71.4) apply(BMI_calc[,2:3], 1, mean) <결과1> [1] 119.35 111.20 133.00 105.10 124.70 #rowMean과 같은 결과가 산출된다. apply(BMI_calc[,2:3], 2, mean) <결과2> height weight 170.20 67.14 #colMean과 같은 결과가 산출된다.
2. sapply(변수, 기술통계값)
- BMI_calc데이터의 2, 3번째 변수(weight, height)를 기준으로 평균을 산출해본다.
#임의 데이터 생성 gender <- c("남", "여", "남", "여", "남") height <- c(170, 165, 183, 155, 178) weight <- c(68.7, 57.4, 83.0, 55.2, 71.4) BMI_calc <- data.frame(gender, height, weight) sapply(BMI_calc[,2:3], mean) <결과1> height weight 170.20 67.14 is(sapply(BMI_calc[,2:3], mean)) <결과2> [1] "numeric" "vector" # 산출 결과가 벡터 형태로 반환됨을 확인할 수 있다.
3. lapply(변수, 기술통계값)
- BMI_calc데이터의 2, 3번째 변수(weight, height)를 기준으로 평균을 산출해본다.
- 각각의 결과가 리스트 형태로 산출됨을 확인할 수 있다.
#임의 데이터 생성 gender <- c("남", "여", "남", "여", "남") height <- c(170, 165, 183, 155, 178) weight <- c(68.7, 57.4, 83.0, 55.2, 71.4) BMI_calc <- data.frame(gender, height, weight) lapply(BMI_calc[,2:3], mean) <결과1> $height [1] 170.2 $weight [1] 67.14 is(lapply(BMI_calc[,2:3], mean)) <결과2> [1] "list" "vector" # list 및 vector 형태의 자료형으로 출력되었음을 알 수 있다.
4. mapply (기술통계값, 변수)
- mapply는 sapply 및 lapply와 순서가 반대이다.
#임의 데이터 생성 gender <- c("남", "여", "남", "여", "남") height <- c(170, 165, 183, 155, 178) weight <- c(68.7, 57.4, 83.0, 55.2, 71.4) BMI_calc <- data.frame(gender, height, weight) mapply(mean, BMI_calc[,2:3]) <결과1> height weight 170.20 67.14 is(mapply(mean, BMI_calc[,2:3])) <결과2> [1] "numeric" "vector" # vector 형태의 자료형으로 반환됨을 알 수 있다.
5. tapply(변수, 집단 구분 변수, 기술통계값)
- tapply는 집단 간 데이터를 구분하여 산출해준다
#임의 데이터 생성 gender <- c("남", "여", "남", "여", "남") height <- c(170, 165, 183, 155, 178) weight <- c(68.7, 57.4, 83.0, 55.2, 71.4) BMI_calc <- data.frame(gender, height, weight) # BMI_calc의 2번쨰 변수(height)를 1번쨰 변수 집단(gender)별로 평균 값을 구해보자 tapply(BMI_calc[,2], BMI_calc[,1], mean) <결과1> 남 여 177 160 # BMI_calc의 3번쨰 변수(weight)를 1번쨰 변수 집단(gender)별로 평균 값을 구해보자 tapply(BMI_calc[,3], BMI_calc[,1], mean) <결과2> 남 여 74.36667 56.30000 is(tapply(BMI_calc[,2], BMI_calc[,1], mean)) <결과3> [1] "array" "structure" "vector" # 데이터가 array 및 vector 형태로 반환됨을 알 수 있다.
<추가> 이전 포스팅에서 진했했던 split + sapply로 tapply 기능 수행하기
#임의 데이터 생성 gender <- c("남", "여", "남", "여", "남") height <- c(170, 165, 183, 155, 178) weight <- c(68.7, 57.4, 83.0, 55.2, 71.4) BMI_calc <- data.frame(gender, height, weight) height_by_gender <- split(BMI_calc$height, BMI_calc$gender) weight_by_gender <- split(BMI_calc$weight, BMI_calc$gender) sapply(height_by_gender, mean) <결과1> 남 여 177 160 sapply(weight_by_gender, mean) <결과2> 남 여 74.36667 56.30000
728x90'R 통계 > R 기초' 카테고리의 다른 글
[R기초] 테이블의 합을 구하는 함수 addmargins() (0) 2021.09.23 [R기초] split 함수로 데이터를 나누고, apply함수로 기술통계 비교하기 (0) 2021.09.16 [R기초] 엑셀의 vlookup과 유사한 함수, merge를 알아보자 (0) 2021.09.15 [R기초] subset 함수를 활용해 원하는 데이터만 선택하자 (0) 2021.09.14 [R기초] colnames 함수를 활용해 변수명 변경하기 (0) 2021.09.13