Rylah's Study & Daily Life

04. R Study 4-1. Function 본문

Study/R

04. R Study 4-1. Function

Rylah 2022. 1. 17. 02:47
## 1. 데이터셋에 사용자 정의 함수 적용하기 ##

res <- function(a,b) {
  res.max <- a
  if (b > a) {
    res.max <- a + b
  }
  return (res.max)
}

res(20,35)
x <- res(20,15)
print(x)
y <- res(22,55)
print((x+y)/2)

Divide <- function(a, b=5) {
  result <- a%/%b
  return(result)
}

Divide(a=30,b=5) #매개변수명과 매개변수 데이터의 묶음으로 입력
Divide(50,10) #매개변수 데이터만 입력력
Divide(100) #a에 대한 데이터만 입력 b는 생략략


Equation <- function(a,b) {
  val.div <- a/b
  val.rmd <- a%%b
  return(list(div=val.div, rmd=val.rmd))
}

result <- Equation(30,10)
d <- result$div # 30, 10의 나누기
r <- result$rmd # 30, 10의 나누기 나머지 값
cat('30/10=', d, '\n')
cat('30%%10=', r, '\n')

source("DATA/Mean.R") #Mean.R 안에 있는 함수 실행

#함수 사용 
a <- mymean(30,5) #mymean 함수 호출
b <- mymean(20,5) #mymean 함수 호출
a-b
mymean(mymean(50,2),5) #mymean 함수에 mymean함수 호출

install.packages("readxl")
library(readxl)

library(dplyr)

subway <- read_excel("DATA/subway.xlsx") #엑셀 파일을 불러와서 subway에 할당

result1 <- subway[subway$역명=="팔당", ]
result2 <- summary(result1)
print(result2)

filter(subway, 노선명=='중앙선')

slice(subway, 10:15)

apply(subway[,4:5], 1, mean) #row 방향 단위로 평균 함수 적용
apply(subway[,4:5], 2, mean) #col 방향 단위로 평균 함수 적용

 

'Study > R' 카테고리의 다른 글

04. R Study 4-2. Find  (0) 2022.01.17
03. R Study 3-2. for, while  (0) 2022.01.17
03 . R Study 3-1. if ,else if , else  (0) 2022.01.17
02. R Study 2-3. File input & output  (0) 2022.01.17
02. R Study 2-2. Data Frame  (0) 2022.01.16