Q2

Find out about the function ‘rep’ by typing? rep and generate a vector containing 10 repetitions of the word “noun”, and another vector containing 10 repetitions of the word “noun” and 20 repetitions of “verb”.

The function of rep

o <- rep(1:5, 3)
o
##  [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
p <- rep(1:5, each =3)
p
##  [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
df_op <- c(o, p)
df_op
##  [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
length(df_op)
## [1] 30
z <- rep(df_op, times =2)
z
##  [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 1 2 3 4 5 1 2 3
## [39] 4 5 1 2 3 4 5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
length(z)
## [1] 60
rep(z, len = 30)
##  [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
df_z <- rep(z, len = 30)
df_z
##  [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
length(df_op)
## [1] 30
length(df_z)
## [1] 30

=> 2 length(df_op) = length(z)

length(df_0p) = length(z)/2 rep(z, len = 30) df_z <- rep(z, len = 30) # Therefore, length(df_op) = length(df_z)

A vector containing 10 repetitions of the word “noun”

a <- rep("noun", 10)
a
##  [1] "noun" "noun" "noun" "noun" "noun" "noun" "noun" "noun" "noun" "noun"
class(a)
## [1] "character"
length(a)
## [1] 10

Another vector containing 10 repetitions of the word “noun” and 20 repetitions of “verb”.

b <- rep("noun", 10)

c <- rep("verb", 20)

noun.and.verb <- c(b, c)

noun.and.verb
##  [1] "noun" "noun" "noun" "noun" "noun" "noun" "noun" "noun" "noun" "noun"
## [11] "verb" "verb" "verb" "verb" "verb" "verb" "verb" "verb" "verb" "verb"
## [21] "verb" "verb" "verb" "verb" "verb" "verb" "verb" "verb" "verb" "verb"

Q3

Find out what the function ‘seq’ does, and generate a user_defined regular sequence.

aa <- seq(1,5)
aa
## [1] 1 2 3 4 5
class(aa)
## [1] "integer"
length(aa)
## [1] 5
bb <- seq(1,100, by = 2)
bb
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
## [26] 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
class(bb)
## [1] "numeric"
length(bb)
## [1] 50
cc <- seq(1,100, by = 5)
cc
##  [1]  1  6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
class(cc)
## [1] "numeric"
length(cc)
## [1] 20

이번 숙제는 repseq 함수를 익히기 위한 것입니다.