기본 연산자
%%: 나머지, %/%: 몫, ** ^:거듭제곱
> 7 %% 2
[1] 1
> 7 %/% 2
[1] 3
> 2 ** 3
[1] 8
> 2^3
[1] 8
소수점 처리
올림
> ceiling(1.1)
[1] 2
내림
> floor(1.1)
[1] 1
반올림
> round(1.1)
[1] 1
> round(1.5)
[1] 2
소수점 반올림
> round(1.111, 1)
[1] 1.1
> round(1.111, 2)
[1] 1.11
소수점 이하 버림
> trunc(1.9999)
[1] 1
반복문
for문
for (i in c(1:5)){
print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
while문
i <- 1
while (i < 5){
i <- i +1
print(i)
}
[1] 2
[1] 3
[1] 4
[1] 5
if문
x <- 3
if (x == 1){
print(1)
}else if (x == 2){
print(2)
}else{
print(3)
}
[1] 3
함수
x,y변수에 할당된 값을 더해주는 함수
xy.sum <- function(x,y){
k = x + y
return(k)
}
xy.sum(2,3)
[1] 5
출력 함수
print()
> print("r studio")
[1] "r studio"
cat()
> cat("r studio")
r studio
print()와 cat()의 차이점: 여러개를 출력할 때, cat은 줄바꿈 하지 않고 출력되지만 print()는 줄바꿈 후 출력이 된다.
> cat("a"); cat("b"); cat("c")
abc
> print("a"); print("b"); print("c")
[1] "a"
[1] "b"
[1] "c"
'Language > R' 카테고리의 다른 글
[R] 다변수정규분포 생성 (mvrnorm) (0) | 2021.03.10 |
---|