2-1. c() 함수
> xVec <- c(11, 22, 33, 44, 55, 66, 77)
> xVec
[1] 11 22 33 44 55 66 77
> mode(xVec)
[1] "numeric"
> yVec <- c("R", "python", "Java", "C", "html")
> yVec
[1] "R" "python" "Java" "C" "html"
> mode(yVec)
[1] "character"
> zVec <- c(T, T, F, F, T)
> zVec
[1] TRUE TRUE FALSE FALSE TRUE
> mode(zVec)
[1] "logical"
2-2. 수열 연산자( : )
> 1004:1020
[1] 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
[13] 1016 1017 1018 1019 1020
> 1020:1004
[1] 1020 1019 1018 1017 1016 1015 1014 1013 1012 1011 1010 1009
[13] 1008 1007 1006 1005 1004
> -10:10
[1] -10 -9 -8 -7- -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10
2-3. seq() 함수
> seq(from = 1, to - 20)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> seq(1, 20)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> seq(1, 20, by=2)
[1] 1 3 5 7 9 11 13 15 17 19
>seq(1, 20, 2)
[1] 1 3 5 7 9 11 13 15 17 19
>seq(1, 20, by=6)
[1] 1 7 13 19
2-4. rep() 함수
> rep(1004, times=5)
[1] 1004 1004 1004 1004 1004
> rep(1:3, times=2)
[1] 1 2 3 1 2 3
> rep(1:3, 2)
[1] 1 2 3 1 2 3
> rep(c(1,2,3), 2)
[1] 1 2 3 1 2 3
> rep(1:3, each=3)
[1] 1 1 1 2 2 2 3 3 3
> rep(1:3, each=3, times=2)
[1] 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3
> rep(c("Y", "N"), c(10, 5))
[1] "Y" "Y" "Y" "Y" "Y" "Y" "Y" "Y" "Y" "Y" "N" "N" "N"
[14] "N" "N"
2-5. paste() 함수와 paste0() 함수
> paste("room", 101:105)
[1] "room 101" "room 102" "room 103" "room 104" "room 105"
> paste("room", 101:105, sep="")
[1] "room101" "room102" "room103" "room104" "room105"
> paste0("room", 101:105)
[1] "room101" "room102" "room103" "room104" "room105"
2-6. replace() 함수
#replace() 함수를 이용한 일부 데이터 변경
> room <- paste("room", 101:105, sep="")
> room
[1] "room101" "room102" "room103" "room104" "room105"
> replace(room, 5, "suiteRoom")
[1] "room101" "room102" "room103" "room104" "suiteRoom"
2-7. append() 함수
> room <- paste("room", 101:105, sep="")
> room
[1] "room101" "room102" "room103" "room104" "room105"
> append(room, "suiteRoom", after=1)
[1] "room101" "suiteRoom" "room102" "room103" "room104" "room105"
> append(room, "suiteRoom")
[1] "room101" "room102" "room103" "room104" "room105" "suiteRoom"
2-8. 첨자 []
> vec <- 1004:1020
> vec
[1] 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
[13] 1016 1017 1018 1019 1020
> vec[5]
[1] 1008
> vec[1:10]
[1] 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
#vec[-1]은 vec의 1번째 원소를 제외한 벡터 생성
> vec[-1]
[1] 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
[13] 1017 1018 1019 1020
> vec[-1:-10]
[1] 1014 1015 1016 1017 1018 1019 1020
> yVec <- c("R", "python", "Java", "C", "html")
> yVec
[1] "R" "python" "Java" "C" "html"
> yVec[2]
[1] "python"
> yVec[1:2]
[1] "R" "python"
> yVec[-5]
[1] "R" "python" "Java" "C"
> yVec[-1:-2]
[1] "Java" "C" "html"
2-9. names() 함수
> score <- c(90, 89, 80, 67, 95)
> names(score) <- c("R", "python", "Java", "C", "html")
> score
R python Java C html
90 89 80 67 95
> score[2]
python
89
> score["python"]
python
89
> score[1:2]
R python
90 89
> score[c("R", "python")]
R python
90 89
2-10. 결측값 NA
> xVec <- c(11, 22, 33, 44, 55, 66, NA)
> xVec
[1] 11 22 33 44 55 66 NA
> is.na(xVec)
[1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE
> mean(xVec)
[1] NA
> mean(xVec, na.rm = TRUE)
[1] 38.5