R의 apply(), lapply(), sapply() 및 tapply()에 대한 가이드
이 튜토리얼에서는 내장 R 함수인 apply() , sapply() , lapply() 및 tapply() 간의 차이점을 설명하고 각 함수를 사용하는 시기와 방법에 대한 예를 설명합니다.
적용하다()
행렬이나 데이터 프레임의 행이나 열에 함수를 적용하려면 apply() 함수를 사용하세요.
Apply() 함수의 기본 구문은 다음과 같습니다.
적용(X, 마진, FUN)
- X는 배열 또는 데이터 블록의 이름입니다.
- MARGIN은 작업을 수행할 차원을 나타냅니다(1 = 행, 2 = 열).
- FUN은 수행하려는 특정 작업입니다(예: 최소, 최대, 합계, 평균 등).
다음 코드는 실행 중인 apply() 의 몇 가지 예를 보여줍니다.
#create a data frame with three columns and five rows data <- data.frame(a = c(1, 3, 7, 12, 9), b = c(4, 4, 6, 7, 8), c = c(14, 15, 11, 10, 6)) data #abc #1 1 4 14 #2 3 4 15 #3 7 6 11 #4 12 7 10 #5 9 8 6 #find the sum of each row apply(data, 1, sum) #[1] 19 22 24 29 23 #find the sum of each column apply(data, 2, sum) #abc #32 29 56 #find the mean of each row apply(data, 1, mean) #[1] 6.333333 7.333333 8.000000 9.666667 7.666667 #find the mean of each column, rounded to one decimal place round(apply(data, 2, mean), 1) #abc #6.4 5.8 11.2 #find the standard deviation of each row apply(data, 1, sd) #[1] 6.806859 6.658328 2.645751 2.516611 1.527525 #find the standard deviation of each column apply(data, 2, sd) #abc #4.449719 1.788854 3.563706
적용하다()
목록, 벡터 또는 데이터 프레임의 각 요소에 함수를 적용하고 결과로 목록을 얻으려면 lapply() 함수를 사용하십시오.
lapply() 함수의 기본 구문은 다음과 같습니다.
라플라이(X, FUN)
- X는 목록, 벡터 또는 데이터 프레임의 이름입니다.
- FUN은 수행하려는 특정 작업입니다.
다음 코드는 데이터 프레임의 열에 lapply()를 사용하는 몇 가지 예를 보여줍니다.
#create a data frame with three columns and five rows data <- data.frame(a = c(1, 3, 7, 12, 9), b = c(4, 4, 6, 7, 8), c = c(14, 15, 11, 10, 6)) data #abc #1 1 4 14 #2 3 4 15 #3 7 6 11 #4 12 7 10 #5 9 8 6 #find mean of each column and return results as a list apply(data, mean) #$a # [1] 6.4 # # $b # [1] 5.8 # # $c # [1] 11.2 #multiply values in each column by 2 and return results as a list lapply(data, function(data) data*2) #$a # [1] 2 6 14 24 18 # # $b # [1] 8 8 12 14 16 # # $c # [1] 28 30 22 20 12
lapply()를 사용하여 목록에 대한 작업을 수행할 수도 있습니다. 다음 예에서는 이를 수행하는 방법을 보여줍니다.
#create a list x <- list(a=1, b=1:5, c=1:10) x #$a # [1] 1 # # $b # [1] 1 2 3 4 5 # # $c # [1] 1 2 3 4 5 6 7 8 9 10 #find the sum of each element in the list lapply(x, sum) #$a # [1] 1 # # $b # [1] 15 # # $c #[1]55 #find the mean of each element in the list lapply(x, mean) #$a # [1] 1 # # $b # [1] 3 # # $c # [1] 5.5 #multiply values of each element by 5 and return results as a list lapply(x, function(x) x*5) #$a # [1] 5 # # $b # [1] 5 10 15 20 25 # # $c # [1] 5 10 15 20 25 30 35 40 45 50
적용하다()
목록, 벡터 또는 데이터 프레임의 각 요소에 함수를 적용하여 목록 대신 벡터를 얻으려면 sapply() 함수를 사용하십시오.
sapply() 함수의 기본 구문은 다음과 같습니다.
적용 (X, FUN)
- X는 목록, 벡터 또는 데이터 프레임의 이름입니다.
- FUN은 수행하려는 특정 작업입니다.
다음 코드는 데이터 프레임의 열에 sapply()를 사용하는 몇 가지 예를 보여줍니다.
#create a data frame with three columns and five rows data <- data.frame(a = c(1, 3, 7, 12, 9), b = c(4, 4, 6, 7, 8), c = c(14, 15, 11, 10, 6)) data #abc #1 1 4 14 #2 3 4 15 #3 7 6 11 #4 12 7 10 #5 9 8 6 #find mean of each column and return results as a vector sapply(data, mean) #abc #6.4 5.8 11.2 #multiply values in each column by 2 and return results as a matrix sapply(data, function(data) data*2) #abc #[1,] 2 8 28 #[2,] 6 8 30 #[3,] 14 12 22 #[4,] 24 14 20 #[5,] 18 16 12
sapply()를 사용하여 목록에 대한 작업을 수행할 수도 있습니다. 다음 예에서는 이를 수행하는 방법을 보여줍니다.
#create a list x <- list(a=1, b=1:5, c=1:10) x #$a # [1] 1 # # $b # [1] 1 2 3 4 5 # # $c # [1] 1 2 3 4 5 6 7 8 9 10 #find the sum of each element in the list sapply(x, sum) #abc #1 15 55 #find the mean of each element in the list sapply(x, mean) #abc #1.0 3.0 5.5
수도꼭지()
벡터의 하위 집합에 함수를 적용하고 하위 집합이 다른 벡터(일반적으로 요인)에 의해 정의되는 경우 tapply() 함수를 사용합니다.
tapply() 함수의 기본 구문은 다음과 같습니다.
탭(X, INDEX, FUN)
- X는 객체의 이름이며 일반적으로 벡터입니다.
- INDEX는 하나 이상의 요소 목록입니다.
- FUN은 수행하려는 특정 작업입니다.
다음 코드는 붓꽃 임베디드 R 데이터 세트에서 tapply() 를 사용하는 예를 보여줍니다 .
#view first six lines of iris dataset head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species #1 5.1 3.5 1.4 0.2 setosa #2 4.9 3.0 1.4 0.2 setosa #3 4.7 3.2 1.3 0.2 setosa #4 4.6 3.1 1.5 0.2 setosa #5 5.0 3.6 1.4 0.2 setosa #6 5.4 3.9 1.7 0.4 setosa #find the max Sepal.Length of each of the three Species tapply(iris$Sepal.Length, iris$Species, max) #setosa versicolor virginica #5.8 7.0 7.9 #find the mean Sepal.Width of each of the three Species tapply(iris$Sepal.Width, iris$Species, mean) # setosa versicolor virginica # 3,428 2,770 2,974 #find the minimum Petal.Width of each of the three Species tapply(iris$Petal.Width, iris$Species, min) # setosa versicolor virginica #0.1 1.0 1.4