티스토리 뷰
R
(macOS)[R] 반복문 : for, while | 조건문 : if/else | 사용자 정의 함수 : function()
jinozpersona 2022. 3. 28. 17:03INTRO
1. 반복문 : iterational code block
2. 조건문 : conditional code block
3. 사용자 정의 함수 : costomized function
1. 반복문
for
for (i in range) {
print(i)
}
while
x = 1
while ( x<num) {
x = x + 1
print(x)
}
2. 조건문
if/else
if (condition) restult_1
else results_2
test_iterational_conditional_code_block.R
rm(list = ls())
setwd("/Users/[YourMacID]/Rcoding")
## for
a = c()
for (i in 1:9) {
a[i] = i * i
cat("i = ",i)
cat(" | a = ",a)
cat("\n")
}
## for
isum = 0
for (i in 1:100) {
isum = isum + i
}
print(isum)
cat("summation of 1 to 100 is ",isum,"\n")
## while
x = 1
while (x < 5) {
print(x)
x = x + 1
}
## if
rand_a = sample(x=1:100, size=40)
over50 = rep(0.40)
for(i in 1:40) {
if (rand_a[i] >= 50) over50[i] = 1
else over50[i] = 0
}
print(rand_a)
print(over50)
cat("count for results : ",sum(over50),"\n")
출력결과
> source("~/Rcoding/test_conditional_code_block.R", echo=TRUE)
> rm(list = ls())
> # setwd("/Users/[YourMacID]/Rcoding")
> setwd("/Users/jiwonlee/Rcoding")
> ## for
> a = c()
> for (i in 1:9) {
+ a[i] = i * i
+ cat("i = ",i)
+ cat(" | a = ",a)
+ cat("\n")
+ }
i = 1 | a = 1
i = 2 | a = 1 4
i = 3 | a = 1 4 9
i = 4 | a = 1 4 9 16
i = 5 | a = 1 4 9 16 25
i = 6 | a = 1 4 9 16 25 36
i = 7 | a = 1 4 9 16 25 36 49
i = 8 | a = 1 4 9 16 25 36 49 64
i = 9 | a = 1 4 9 16 25 36 49 64 81
> ## for
> isum = 0
> for (i in 1:100) {
+ isum = isum + i
+ }
> print(isum)
[1] 5050
> cat("summation of 1 to 100 is ",isum,"\n")
summation of 1 to 100 is 5050
> ## while
> x = 1
> while (x < 5) {
+ print(x)
+ x = x + 1
+ }
[1] 1
[1] 2
[1] 3
[1] 4
> ## if
> rand_a = sample(x=1:100, size=40)
> over50 = rep(0.40)
> for(i in 1:40) {
+ if (rand_a[i] >= 50) over50[i] = 1
+ else over50[i] = 0
+ }
> print(rand_a)
[1] 95 85 70 7 49 78 2 100 21 66 12 93 34 43 50 51 99 65 86 63 79 52 1 37 69 80 59
[28] 39 8 71 62 53 77 16 58 4 14 35 96 56
> print(over50)
[1] 1 1 1 0 0 1 0 1 0 1 0 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 1 1
> cat("count for results : ",sum(over50),"\n")
count for results : 25
3. 사용자 정의 함수
fun_name = function(args) {
code_blocks
}
test_costomized_function.R
rm(list = ls())
setwd("/Users/[YourMacID]/Rcoding")
## define function
addto = function(x) {
isum = 0
for (i in 1:x) {
isum = isum + i
}
print(isum)
}
addto(100)
addto(50)
출력결과
> source("~/Rcoding/test_costomized_function.R", echo=TRUE)
> rm(list = ls())
> setwd("/Users/[YourMacID]/Rcoding")
> ## define function
> addto = function(x) {
+ isum = 0
+ for (i in 1:x) {
+ isum = isum + i
+ }
+ print(isum)
+ }
> addto(100)
[1] 5050
> addto(50)
[1] 1275
반응형
'R' 카테고리의 다른 글
(macOS)[R] plot : scatter plot, histogram, box plot (0) | 2022.03.29 |
---|---|
(macOS)[R] paste, substr, as., date, format (0) | 2022.03.28 |
(macOS)[R] 데이터 다루기(data handle) (0) | 2022.03.28 |
(macOS)[R] 기초함수, 수치계산 (0) | 2022.03.28 |
(macOS)[R] file import : csv, txt, xls, xlsx (0) | 2022.03.15 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- r
- git
- MacOS
- pyserial
- DAQ
- ERP
- 확진
- COVID-19
- Python
- Model
- template
- Django
- 자가격리
- vscode
- 라즈베리파이
- sublime text
- Pandas
- DS18B20
- analysis
- github
- 코로나
- 코로나19
- CSV
- raspberrypi
- arduino
- Regression
- server
- SSH
- Templates
- Raspberry Pi
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함