"R apply()"의 두 판 사이의 차이

3번째 줄: 3번째 줄:
* 행렬에 대해 행(또는 열) 방향으로 원하는 함수를 적용하는 데 사용하는 R 함수
* 행렬에 대해 행(또는 열) 방향으로 원하는 함수를 적용하는 데 사용하는 R 함수


==행렬 예시==
<source lang='r' notebook>
<source lang='r' notebook>
x <- matrix(1:12,3,4)
x <- matrix(1:12,3,4)
20번째 줄: 21번째 줄:
</source>
</source>


<source lang='r' run>
==데이터프레임 예시==
mydf <- read.table( header=TRUE, stringsAsFactors=FALSE, text="
<source lang='r' notebook=2>
df <- read.table( header=TRUE, stringsAsFactors=FALSE, text="
English Math
English Math
     30  60
     30  60
28번째 줄: 30번째 줄:
     60  90
     60  90
")
")
options(echo=T)
df
mydf
</source>
 
<source lang='r' notebook=2>
rowMeans(mydf)
rowMeans(mydf)
</source>
<source lang='r' notebook=2>
apply(mydf, 1, mean)
apply(mydf, 1, mean)
 
</source>
<source lang='r' notebook=2>
colMeans(mydf)
colMeans(mydf)
</source>
<source lang='r' notebook=2>
sapply(mydf, mean)
sapply(mydf, mean)
</source>
<source lang='r' notebook=2>
apply(mydf, 2, mean)
apply(mydf, 2, mean)
</source>
</source>

2021년 4월 17일 (토) 15:21 판

1 개요

R apply()
  • 행렬에 대해 행(또는 열) 방향으로 원하는 함수를 적용하는 데 사용하는 R 함수

2 행렬 예시

x <- matrix(1:12,3,4)
x
apply(x, 1, sum) ## 가로 합
apply(x, 2, sum) ## 세로 합
apply(x, 1, mean) ## 가로 평균
apply(x, 2, mean) ## 세로 평균

3 데이터프레임 예시

df <- read.table( header=TRUE, stringsAsFactors=FALSE, text="
English Math
     30   60
     40   70
     50   80
     60   90
")
df
rowMeans(mydf)
apply(mydf, 1, mean)
colMeans(mydf)
sapply(mydf, mean)
apply(mydf, 2, mean)

4 같이 보기

5 참고

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