📔설명
빅데이터분석기사 실기 준비를 위해 R 문법을 공부해보자!
🧇연산자
help()/?연산자 |
---|
함수명, 데이터셋에 대한 설명 해주는 도움말 함수 |

0은 FALSE, 그 외는 TRUE
🧀자료형
mode(x) |
---|
x 객체의 형식(자료형)이 무엇인지 알려주는 함수 |

typeof(x) |
---|
객체의 형식이 무엇인지 알려주는 함수 |
mode함수
에서 numeric
으로 출력되는 값이
정수형
일 경우 integer
, 실수형
일 경우 double
, 나머지는 mode
와 같음
intger
가 되도록 출력시 5L
처럼 뒤에 L
붙여줘야 함

🧂벡터(Vector)
같은 자료형
을 가진 원소들만 저장할 수 있는 자료형

rep(x, times) |
---|
x벡터를 times만큼 반복 |

seq(from,to,by) |
---|
from 시작값 부터 to 끝값까지 by간격으로 증감 |

- 벡터[n] : 벡터의 n번째 값
- 벡터[-n] : 벡터 n번째 값 제외 모든 요소
- 벡터[조건문] : 조건문 만족 요소 반환
- 벡터[a:b] : a번째 요소부터 b번째 요소까지 반환

벡터함수 | - |
---|
sqrt(x) | 제곱근 |
log(x,base) | x는 로그, base는 밑 , base 기본은 e (logeX) |
trunc(x) | 소수점 버림 |
range(x) | 범위 출력 |
sd(x) | 표준편차 |
cov(x,y) | 공분산 |
cor(x,y) | 상관계수 |
🥩리스트(List)
모든 객체를 담을 수 있는 데이터 구조
list(key=value, key=value) |
---|
리스트 생성 |

🌯행렬(Matrix)
2차원의 벡터
matrix(data, nrow, ncol, byrow, dimnames) |
---|
data : 저장할 데이터 nrow: 행의 개수 ncol : 열의 개수 byrow : 행렬의 데이터 입력 순서(기본 F), T는 행을 기준 dimnames : 행과 열 이름 리스트 |

행렬 함수 | - |
---|
dim(x) | x행렬의 차원 확인 |
dim(x)<-c(m,n) | x벡터/행렬을 m x n 행렬로 변환 |
nrow(x) | x행렬 행의 수 |
ncol(x) | x행렬 열의 수 |
x[m,n] | m행 n열 원소 추출 |
x[m,] | x행렬의 m행 |
x[,n] | n열 |
rownames(x) | x행렬의 행 이름 출력 |
colnames(x) | x행렬의 열 이름 출력 |
rowSums(x) | 행 합계 |
colSums(x) | 열 합계 |


🥡데이터 프레임(Data Frame)
각 변수가 서로 다른 자료형 가짐
data.frame(변수명=벡터, stringAsFactors) |
---|
변수명=벡터 : 변수명과 해당 열에 저장할 벡터 지정 stringAsFactors : 주어진 문자열을 요인으로 저장할지 여부 |

🍧요인(Factor)
범주형 자료를 표현
facotr(data, levels, labels, ordered) |
---|
data : 범주형 데이터 levels : 구분하고자 하는 범주(레벨) labels : 범주별 표시 값 ordered : 순서형인지 명목형인지 지정 |

🥛조건문
if문 |
---|
if(조건문){ 명령어 }else if(조건문){ 명령어 }else{ 명령어 } |

ifelse문
: 단순히 참과 거짓인 경우
ifelse(조건식, 명령어1, 명령어2) |
---|
조건식 참이면 명령어1, 아니면 명령어2 |

switch문
: 조건에 따라 여러 경로 중 하나 선택해 명령어 실행
switch(변수,유형1=명령어1 ,...,) |
---|
변수 값이 유형1이면 명령어1 실행 등.. |

🍥반복문
for(i in temp){ 명령어 } |
---|
temp의 각각 값을 변수 i에 할당하고 블록 안 문장 수행 |

while(조건문){ 명령어 } |
---|
조건문이 참인 경우 반복 |


🥞루프 제어 명령어
break
: 반복문 중간에 탈출
next
: 반복문에서 다음 반복으로 넘어감 (==continue)
🍍사용자 정의 함수
함수명 = function(인자, ..) { .. return(값)} |
---|
값 반환 사용자 정의 함수 |

함수명 = function(인자, ..) { ..} |
---|
반환 값이 없는 사용자 정의 함수. 마지막 실행 결과 반환 |
