01. 다음 각 문제를 해결하기 위한 R 코드를 작성하시오.

  1. 1~100사이의 정수 중 3의 배수들의 합과 개수를 구하시오.
sum(seq(3,100,3))
## [1] 1683
length(seq(3,100,3))
## [1] 33
  1. 101 ~ 200사이의 숫자 중 3과 4의 공배수를 구하시오.
for (i in 101:200){
  if(i%%3==0 & i%%4==0){
  print(i)
}
}
## [1] 108
## [1] 120
## [1] 132
## [1] 144
## [1] 156
## [1] 168
## [1] 180
## [1] 192
  1. 약수란 나누어 떨이지는 수를 의미한다. 이 때 24의 약수를 구하시오.
for (i in 1:24){
  if(24%%i==0){
    print(i)
  }
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 6
## [1] 8
## [1] 12
## [1] 24
  1. 10!를 출력하시오(단, factorial() 함수를 이용하지 않는다.)
mul <- 1
for (i in 1:10){
  mul <- mul*i
}
mul
## [1] 3628800

02. 다음을 해결하기 위한 R code를 작성하시오.

  1. month.name을 실행하여 결과를 확인하시오.
month.name
##  [1] "January"   "February"  "March"     "April"     "May"       "June"     
##  [7] "July"      "August"    "September" "October"   "November"  "December"
  1. for 문을 이용하여 month.name 벡터로 부터 다음과 같이 월 이름을 출력하시오.
The month of January
The month of February
The month of March
...(중략)
The month of October
The month of Novemer
The month of December
for(i in 1:12){
  cat("The month of",month.name[i],'\n')
}
## The month of January 
## The month of February 
## The month of March 
## The month of April 
## The month of May 
## The month of June 
## The month of July 
## The month of August 
## The month of September 
## The month of October 
## The month of November 
## The month of December

03. for 문을 이용하여 구구단 중 9단을 출력하는 R 코드를 작성하시오.

for(i in 1:9){
  cat("9 x",i,"=",9*i,'\n')
}
## 9 x 1 = 9 
## 9 x 2 = 18 
## 9 x 3 = 27 
## 9 x 4 = 36 
## 9 x 5 = 45 
## 9 x 6 = 54 
## 9 x 7 = 63 
## 9 x 8 = 72 
## 9 x 9 = 81

04. while문을 이용하여 구구단 중 8단을 출력하는 R 코드를 작성하시오.

i <- 1
while (i<10) {
  cat("8 x",i,"=",8*i,'\n')
  i <- i+1
}
## 8 x 1 = 8 
## 8 x 2 = 16 
## 8 x 3 = 24 
## 8 x 4 = 32 
## 8 x 5 = 40 
## 8 x 6 = 48 
## 8 x 7 = 56 
## 8 x 8 = 64 
## 8 x 9 = 72

05. 1~100의 정수를 차례로 출력하되 3의 배수에서는 숫자 대신 ‘*’을 출력하는 R 코드를 작성하시오(출력은 가로방향으로 한다).

for(i in 1:100){
  if (i%%3==0){            # result <- ifelse(i%%3==0, '*', i)
    cat("*"," ")           # cat(result, ' ')
  }else{cat(i," ")}
}
## 1  2  *  4  5  *  7  8  *  10  11  *  13  14  *  16  17  *  19  20  *  22  23  *  25  26  *  28  29  *  31  32  *  34  35  *  37  38  *  40  41  *  43  44  *  46  47  *  49  50  *  52  53  *  55  56  *  58  59  *  61  62  *  64  65  *  67  68  *  70  71  *  73  74  *  76  77  *  79  80  *  82  83  *  85  86  *  88  89  *  91  92  *  94  95  *  97  98  *  100

06. R의 apply() 함수를 이용하여 다음 문제를 해결하는 R 코드를 작성하시오.

  1. iris 데이터셋의 행별 합계를 출력하시오.
apply(iris[,1:4],1,sum)
##   [1] 10.2  9.5  9.4  9.4 10.2 11.4  9.7 10.1  8.9  9.6 10.8 10.0  9.3  8.5 11.2
##  [16] 12.0 11.0 10.3 11.5 10.7 10.7 10.7  9.4 10.6 10.3  9.8 10.4 10.4 10.2  9.7
##  [31]  9.7 10.7 10.9 11.3  9.7  9.6 10.5 10.0  8.9 10.2 10.1  8.4  9.1 10.7 11.2
##  [46]  9.5 10.7  9.4 10.7  9.9 16.3 15.6 16.4 13.1 15.4 14.3 15.9 11.6 15.4 13.2
##  [61] 11.5 14.6 13.2 15.1 13.4 15.6 14.6 13.6 14.4 13.1 15.7 14.2 15.2 14.8 14.9
##  [76] 15.4 15.8 16.4 14.9 12.8 12.8 12.6 13.6 15.4 14.4 15.5 16.0 14.3 14.0 13.3
##  [91] 13.7 15.1 13.6 11.6 13.8 14.1 14.1 14.7 11.7 13.9 18.1 15.5 18.1 16.6 17.5
## [106] 19.3 13.6 18.3 16.8 19.4 16.8 16.3 17.4 15.2 16.1 17.2 16.8 20.4 19.5 14.7
## [121] 18.1 15.3 19.2 15.7 17.8 18.2 15.6 15.8 16.9 17.6 18.2 20.1 17.0 15.7 15.7
## [136] 19.1 17.7 16.8 15.6 17.5 17.8 17.4 15.5 18.2 18.2 17.2 15.7 16.7 17.3 15.8
  1. iris 데이터셋의 열별 최댓값을 출력하시오.
apply(iris[,1:4],2,max)
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
##          7.9          4.4          6.9          2.5

07. R의 apply() 함수를 이용하여 다음 문제를 해결하는 R 코드를 작성하시오.

  1. mtcars 데이터셋의 열별 합계를 출력하시오.
apply(mtcars,2,sum)
##      mpg      cyl     disp       hp     drat       wt     qsec       vs 
##  642.900  198.000 7383.100 4694.000  115.090  102.952  571.160   14.000 
##       am     gear     carb 
##   13.000  118.000   90.000
  1. mtcars 데이터셋의 열별 최댓값을 출력하시오.
apply(mtcars,2,max)
##     mpg     cyl    disp      hp    drat      wt    qsec      vs      am    gear 
##  33.900   8.000 472.000 335.000   4.930   5.424  22.900   1.000   1.000   5.000 
##    carb 
##   8.000
  1. mtcars 데이터셋의 열별 표준편차를 출력하시오. 표준편차를 구하는 함수는 sd()이다.
apply(mtcars,2,sd)
##         mpg         cyl        disp          hp        drat          wt 
##   6.0269481   1.7859216 123.9386938  68.5628685   0.5346787   0.9784574 
##        qsec          vs          am        gear        carb 
##   1.7869432   0.5040161   0.4989909   0.7378041   1.6152000

08. 다음과 같이 두 정수를 입력하면 두 수의 최대공약수를 찾아서 반환(return)하는 R 함수 lgm()을 만들고 아래와 같은 결과를 내는지 테스트하시오.

> result <- lgm(10,8)  
> result    
[1] 2    
> result <- lgm(10,20)  
> result  
[1] 10  
lgm <- function(x,y){
  for(i in 1:x){
    if(x%%i==0 & y%%i==0){
    max.common <- i
    }
  }
  return(max.common)
}

result <- lgm(10,8)
result
## [1] 2
result <- lgm(10,20)
result
## [1] 10

09. 다음과 같이 벡터를 입력하면 벡터의 최댓값과 최솟값을 반환(return)하는 R 함수 maxmin()을 만들고 아래와 같은 결과를 내는지 테스트하시오(반환 값은 list임).

> v1 <- c(7,1,2,8,9)   
> result <- maxmin(v1)  
> result$max ; result$min  
[1] 9  
[1] 1  
> result <- maxmin(iris[,1])  
> result$max ; result$min  
[1] 7.9  
[1] 4.3  
maxmin <- function(v){
  a <- max(v)
  b <- min(v)
  return(list(max=a,min=b))
}

v1 <- c(7,1,2,8,9)
result <- maxmin(v1)
result$max; result$min
## [1] 9
## [1] 1
result <- maxmin(iris[,1])
result$max; result$min
## [1] 7.9
## [1] 4.3

10. 10명의 몸무게를 저장한 벡터가 다음과 같을 때 질문을 해결하기 위한 R 코드를 작성하시오. which() 함수 계열을 이용하시오.

weight <- c(69, 50, 55, 71, 89, 64, 59, 70, 71, 80)

  1. 몸무게가 가장 큰 값은 몇 번째에 있나?
weight <- c(69, 50, 55, 71, 89, 64, 59, 70, 71, 80)
which.max(weight)
## [1] 5
  1. 몸무게가 가장 작은 값은 몇 번째에 있나?
which.min(weight)
## [1] 2
  1. 몸무게가 61에서 69 사이인 값들은 몇 번째에 있나?
which(weight>=61 & weight<=69)
## [1] 1 6
  1. 몸무게가 60 이하인 값들만 추출하여 weight.2에 저장하고 내용을 보이시오.
weight.2 <- weight[which(weight<=60)]
weight.2
## [1] 50 55 59

11. 다음의 문제를 해결하기 위한 R 코드를 작성하시오(which() 함수 계열을 사용).

  1. iris 데이터셋에서 꽃잎의 길이(Petal.Length)가 가장 큰 행의 내용을 보이시오.
iris[which.max(iris$Petal.Length),]
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 119          7.7         2.6          6.9         2.3 virginica
  1. iris 데이터셋에서 꽃잎의 폭(Petal.Width)이 0.3~0.4 사이인 행들의 내용을 보이시오.
iris[which(iris$Petal.Width>=0.3 & iris$Petal.Width<=0.4),]
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 6           5.4         3.9          1.7         0.4  setosa
## 7           4.6         3.4          1.4         0.3  setosa
## 16          5.7         4.4          1.5         0.4  setosa
## 17          5.4         3.9          1.3         0.4  setosa
## 18          5.1         3.5          1.4         0.3  setosa
## 19          5.7         3.8          1.7         0.3  setosa
## 20          5.1         3.8          1.5         0.3  setosa
## 22          5.1         3.7          1.5         0.4  setosa
## 27          5.0         3.4          1.6         0.4  setosa
## 32          5.4         3.4          1.5         0.4  setosa
## 41          5.0         3.5          1.3         0.3  setosa
## 42          4.5         2.3          1.3         0.3  setosa
## 45          5.1         3.8          1.9         0.4  setosa
## 46          4.8         3.0          1.4         0.3  setosa

12. 인수로 주어진 문자를 텍스트로 부터 제거하는 함수를 작성하시오. 작성한 함수를 이용하여 다음 텍스트 벡터로 부터 세미콜론을 제거하시오.

I saw her standing there; Misery; Anna (Go to him); Chains; Boys; Ask me why
remove <- function(st,pattern){
  st <- gsub(pattern,"",st)
  return(st)
}

remove("I saw her standing there; Misery; Anna (Go to him); Chains; Boys; Ask me why",";")
## [1] "I saw her standing there Misery Anna (Go to him) Chains Boys Ask me why"

13. 데이터프레임 또는 행렬을 인수로 받아서 각 열의 데이터 유형을 출력하는 함수를 작성하시오. iris 데이터넷과 LakeHuron 데이터셋에 대해 테스트하여 각각 다음과 같은 형식으로 출력하시오.

Sepal.Length is numeric 
Sepal.Width is numeric 
Petal.Length is numeric 
Petal.Width is numeric 
Species is factor 
It is neither a data frame nor a matrix.
cla <- function(data){
  name <- colnames(data)
  tryCatch(
    for(i in 1:length(data)){
      cat(name[i],"is",class(data[,i]),'\n')
    }
    , error=function(e) cat("It is neither a data frame nor a matrix.")
  )
}
cla(iris)
## Sepal.Length is numeric 
## Sepal.Width is numeric 
## Petal.Length is numeric 
## Petal.Width is numeric 
## Species is factor
cla(LakeHuron)
## It is neither a data frame nor a matrix.

14. 짝수이면 TRUE를, 홀수이면 FALSE를 출력하는 함수를 작성하고 다음 벡터에 대해 테스트하여 결과를 제시하시오.

c(-5:5, Inf, -Inf, NA, NaN)
isEven <- function(x){
  (x %% 2) == 0
}
isEven(c(-5:5, Inf, -Inf, NA, NaN))
##  [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE    NA
## [13]    NA    NA    NA

15. 짝수의 갯수를 세는 함수를 작성하고, 다음 벡터에 대해 테스트하시오.

c(-5:5, Inf, -Inf, NA, NaN)
count <- 0
count.Even <- function(x){
  for(val in x){
    if(!is.na(val%%2==0) & (val%%2==0))
      count <- count+1
  }
  print(count)
}
count.Even(c(-5:5, Inf, -Inf, NA, NaN))
## [1] 5