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

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 하나는 보이지 않습니다)
4번째 줄: 4번째 줄:


==행렬 예시==
==행렬 예시==
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
x <- matrix(1:12,3,4)
x <- matrix(1:12,3,4)
x
x
</source>
</syntaxhighlight>
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
apply(x, 1, sum) ## 가로 합
apply(x, 1, sum) ## 가로 합
</source>
</syntaxhighlight>
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
apply(x, 2, sum) ## 세로 합
apply(x, 2, sum) ## 세로 합
</source>
</syntaxhighlight>
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
apply(x, 1, mean) ## 가로 평균
apply(x, 1, mean) ## 가로 평균
</source>
</syntaxhighlight>
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
apply(x, 2, mean) ## 세로 평균
apply(x, 2, mean) ## 세로 평균
</source>
</syntaxhighlight>


==데이터프레임 예시==
==데이터프레임 예시==
<source lang='r' notebook=2>
<syntaxhighlight lang='r' notebook=2>
df <- read.table( header=TRUE, stringsAsFactors=FALSE, text="
df <- read.table( header=TRUE, stringsAsFactors=FALSE, text="
English Math
English Math
31번째 줄: 31번째 줄:
")
")
df
df
</source>
</syntaxhighlight>
<source lang='r' notebook=2>
<syntaxhighlight lang='r' notebook=2>
apply(df, 1, mean) # 가로 평균
</syntaxhighlight>
<syntaxhighlight lang='r' notebook=2>
rowMeans(df)
rowMeans(df)
</source>
</syntaxhighlight>
<source lang='r' notebook=2>
<syntaxhighlight lang='r' notebook=2>
apply(df, 1, mean)
apply(df, 2, mean) # 세로 평균
</source>
</syntaxhighlight>
<source lang='r' notebook=2>
<syntaxhighlight lang='r' notebook=2>
colMeans(df)
colMeans(df)
</source>
</syntaxhighlight>
<source lang='r' notebook=2>
<syntaxhighlight lang='r' notebook=2>
sapply(df, mean)
sapply(df, mean)
</source>
</syntaxhighlight>
<source lang='r' notebook=2>
apply(df, 2, mean)
</source>


==같이 보기==
==같이 보기==

2021년 4월 17일 (토) 15:30 기준 최신판

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
apply(df, 1, mean) # 가로 평균
rowMeans(df)
apply(df, 2, mean) # 세로 평균
colMeans(df)
sapply(df, mean)

4 같이 보기[ | ]

5 참고[ | ]

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