함수 작성

금융을 위한 R 중급

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

함수 구조

func_name <- function(arguments) {
  body
}
금융을 위한 R 중급

1 더하기

add_one <- function(x) {
  x_plus_one <- x + 1
  return(x_plus_one)
}

add_one(7)
8
add_one <- function(x) {
  x + 1
}

add_one(7)
8
금융을 위한 R 중급

선택적 인자 사용

add <- function(x, value = 1) {
  x + value
}

add(7)
8
add(7, value = 3)
10
금융을 위한 R 중급

산술 수익률 계산

ch4_vid2_slides.027.png

prices <- c(23.4, 23.8, 22.3)

# S_(t) - S(t-1) vector diff(prices)
0.4 -1.5
# S_(t-1) vector
prices[-length(prices)]
23.4 23.8
# 산술 수익률
diff(prices) / prices[-length(prices)]
0.01709402 -0.06302521
금융을 위한 R 중급

산술 수익률 계산

prices <- c(23.4, 23.8, 22.3)

arith_returns <- function(x) { diff(x) / x[-length(x)] }
arith_returns(prices)
0.01709402 -0.06302521
금융을 위한 R 중급

연습해 봅시다!

금융을 위한 R 중급

Preparing Video For Download...