Skip to content

Instantly share code, notes, and snippets.

@enujo
Created July 26, 2017 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enujo/092cbcc6f31cbdf59ff147ddca8ff932 to your computer and use it in GitHub Desktop.
Save enujo/092cbcc6f31cbdf59ff147ddca8ff932 to your computer and use it in GitHub Desktop.
setwd("D:/BigData/source")
getwd()
#전치 행렬 : 행과 열을 바꾼 행렬
m <- matrix(c(1:9),nrow=3)
m
t(m)
m
# 하삼각행렬 : lower.tri()
lm <- lower.tri(m)
lm <- lower.tri(m, diag=F)
lm
m[lm] # true 인 데이터만 나온다
m
class(m[lm] )
# True 값을 갖는 위치에 0을 입력하고 False인 곳은 값을 출력
# 사실은 상삼각행렬이 된다
m[lm] <- c(0)
m
# 상삼각행렬 : upper.tri(행렬, diag=F) diag 직선(true면 대각선까지)
m <- matrix(c(1:9),nrow=3)
um <- upper.tri(m, diag=F)
um
m[um] <- c(0)
m
m
# 대각행렬 : 대각 성분 이외의 모든 요소들이 0값을 갖음
diagM <- c(1:4)
diagM
diag(diagM)
# 대칭행렬 : 원행렬과 전치행렬이 일치하는 행렬 랜덤하게 값을 가져오던가 흩어질때
m <- matrix(c(1:9), nrow=3)
m
m[lower.tri(m)] <- c(0)
m
n <- t(m)
n
n[upper.tri(n,diag=F)] <- c(0)
n
sysM <- m+n
sysM
t(sysM)
applyData <- matrix(c(1:20), nrow=5)
applyData
# 행단위의 합계를 벡터로 출력 : apply(데이터, 행|열, 함수)
apply(applyData, 1, sum)
# 열단위의 합계를 벡터로 출력
apply(applyData, 2, sum)
# 행단위의 평균을 벡터로 출력
apply(applyData, 1,mean)
# 열단위의 평균을 벡터로 출력
apply(applyData, 2,mean)
# 사용자 함수 생성 : userFunc
# 프로세스 : 벡터 데이터를 입력 받아서 데이터의 두배값을 리턴하는 함수
userFunc <- function(x){
return (x*2)
}
result <- userFunc()
result
apply(applyData,1,userFunc)
apply(applyData,2,userFunc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment