Sys.Date()

for(조건) 반복문

gop<-matrix(gop)

ls()
## character(0)
rm(gop)
## Warning in rm(gop): 객체 'gop'를 찾을 수 없습니다
gop<-""

for( i in 1:9 )
 {
  for( j in 1:9 )
  {
    print(paste(i, "*", j, "=", i*j)) ;
    multi <- paste(i, "*", j, "=", i*j) ;
    gop <- c(gop, multi)
  }
}
## [1] "1 * 1 = 1"
## [1] "1 * 2 = 2"
## [1] "1 * 3 = 3"
## [1] "1 * 4 = 4"
## [1] "1 * 5 = 5"
## [1] "1 * 6 = 6"
## [1] "1 * 7 = 7"
## [1] "1 * 8 = 8"
## [1] "1 * 9 = 9"
## [1] "2 * 1 = 2"
## [1] "2 * 2 = 4"
## [1] "2 * 3 = 6"
## [1] "2 * 4 = 8"
## [1] "2 * 5 = 10"
## [1] "2 * 6 = 12"
## [1] "2 * 7 = 14"
## [1] "2 * 8 = 16"
## [1] "2 * 9 = 18"
## [1] "3 * 1 = 3"
## [1] "3 * 2 = 6"
## [1] "3 * 3 = 9"
## [1] "3 * 4 = 12"
## [1] "3 * 5 = 15"
## [1] "3 * 6 = 18"
## [1] "3 * 7 = 21"
## [1] "3 * 8 = 24"
## [1] "3 * 9 = 27"
## [1] "4 * 1 = 4"
## [1] "4 * 2 = 8"
## [1] "4 * 3 = 12"
## [1] "4 * 4 = 16"
## [1] "4 * 5 = 20"
## [1] "4 * 6 = 24"
## [1] "4 * 7 = 28"
## [1] "4 * 8 = 32"
## [1] "4 * 9 = 36"
## [1] "5 * 1 = 5"
## [1] "5 * 2 = 10"
## [1] "5 * 3 = 15"
## [1] "5 * 4 = 20"
## [1] "5 * 5 = 25"
## [1] "5 * 6 = 30"
## [1] "5 * 7 = 35"
## [1] "5 * 8 = 40"
## [1] "5 * 9 = 45"
## [1] "6 * 1 = 6"
## [1] "6 * 2 = 12"
## [1] "6 * 3 = 18"
## [1] "6 * 4 = 24"
## [1] "6 * 5 = 30"
## [1] "6 * 6 = 36"
## [1] "6 * 7 = 42"
## [1] "6 * 8 = 48"
## [1] "6 * 9 = 54"
## [1] "7 * 1 = 7"
## [1] "7 * 2 = 14"
## [1] "7 * 3 = 21"
## [1] "7 * 4 = 28"
## [1] "7 * 5 = 35"
## [1] "7 * 6 = 42"
## [1] "7 * 7 = 49"
## [1] "7 * 8 = 56"
## [1] "7 * 9 = 63"
## [1] "8 * 1 = 8"
## [1] "8 * 2 = 16"
## [1] "8 * 3 = 24"
## [1] "8 * 4 = 32"
## [1] "8 * 5 = 40"
## [1] "8 * 6 = 48"
## [1] "8 * 7 = 56"
## [1] "8 * 8 = 64"
## [1] "8 * 9 = 72"
## [1] "9 * 1 = 9"
## [1] "9 * 2 = 18"
## [1] "9 * 3 = 27"
## [1] "9 * 4 = 36"
## [1] "9 * 5 = 45"
## [1] "9 * 6 = 54"
## [1] "9 * 7 = 63"
## [1] "9 * 8 = 72"
## [1] "9 * 9 = 81"
gop <- as.factor(gop)
gop
##  [1]            1 * 1 = 1  1 * 2 = 2  1 * 3 = 3  1 * 4 = 4  1 * 5 = 5 
##  [7] 1 * 6 = 6  1 * 7 = 7  1 * 8 = 8  1 * 9 = 9  2 * 1 = 2  2 * 2 = 4 
## [13] 2 * 3 = 6  2 * 4 = 8  2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16
## [19] 2 * 9 = 18 3 * 1 = 3  3 * 2 = 6  3 * 3 = 9  3 * 4 = 12 3 * 5 = 15
## [25] 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 4 * 1 = 4  4 * 2 = 8 
## [31] 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32
## [37] 4 * 9 = 36 5 * 1 = 5  5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
## [43] 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 6 * 1 = 6  6 * 2 = 12
## [49] 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48
## [55] 6 * 9 = 54 7 * 1 = 7  7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35
## [61] 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 8 * 1 = 8  8 * 2 = 16
## [67] 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
## [73] 8 * 9 = 72 9 * 1 = 9  9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45
## [79] 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
## 82 Levels:  1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 ... 9 * 9 = 81
gopdel <-gop[c(2:82)]
gop1dan <-gopdel[c(1:9)]


#81개의 factor요소를 9행 9열의 행렬로 변환.............
gopmat <- matrix(gopdel, nrow=9, ncol=9)

gopmat[,1]
## [1] "1 * 1 = 1" "1 * 2 = 2" "1 * 3 = 3" "1 * 4 = 4" "1 * 5 = 5" "1 * 6 = 6"
## [7] "1 * 7 = 7" "1 * 8 = 8" "1 * 9 = 9"
#문자 연결 함수 paste의 인수 sep는 띄어쓰기 구분바 지정에 사용되는데,
#구분자 없이 붙여쓸때는 sep=""와 동일하며
#paste0()  함수로 구분자 없이 사용 가능하다.

#paste(x, "번째", sep=" ")

#paste0()  : 아래와 같이 벡터 다중 반복 작업도 가능
paste0(1:12, c("st", "nd", "rd", rep("th", 9)))
##  [1] "1st"  "2nd"  "3rd"  "4th"  "5th"  "6th"  "7th"  "8th"  "9th"  "10th"
## [11] "11th" "12th"
# while(조건) 반복문

y <- c(1, 5)
x <- y/2

while (any(abs(x*x-y) > 1e-10)) 
{
  x <- (x + y/x)/2 
  }

x
## [1] 1.000000 2.236068
x^2
## [1] 1 5
# repeat{expression; break}

y <- c(1, 5)
x <- y/2
repeat{
  x <- (x + y/x)/2
  if (any(abs(x*x-y) < 1e-10)) break
  }

x
## [1] 1.000000 2.236068
x^2
## [1] 1 5
# for loops over a fixed set
x <- seq(0, 1, 0.01)
plot(x, x, ylab="y", type="l")
for (j in 1:20) 
  lines(x, x^j)

#tip   <-과    =의 차
#The operators <- and = assign into the environment in which they are evaluated. 
#The operator <- can be used anywhere, whereas the operator = is only allowed at the top level
#(e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.


#---------------------------------------------------------
#tip 실행시간 측정
ptm <- proc.time()

# 로직

x <- 1:100000

for (i in seq_along(x))  x[i] <- x[i]+1



proc.time() - ptm
##    user  system elapsed 
##    0.19    0.00    0.18
#---------------------------------------------------------