답안.hwp
닫기[1번 풀이]
> (y<-seq(from=1.0, to=10.0, by=0.5))
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5
[15] 8.0 8.5 9.0 9.5 10.0
> (x<-log(y))
[1] 0.0000000 0.4054651 0.6931472 0.9162907 1.0986123 1.2527630 1.3862944
[8] 1.5040774 1.6094379 1.7047481 1.7917595 1.8718022 1.9459101 2.0149030
[15] 2.0794415 2.1400662 2.1972246 2.2512918 2.3025851
[2번 풀이]
> (z<-rnorm(20))
[1] -0.646256284 0.047788177 0.672757792 1.082015000 -0.744174321
[6] 0.520530947 0.817194128 0.798935979 -1.282130854 -0.009496196
[11] 0.582656524 -0.666649444 -0.611830854 -0.241628250 -0.768706526
[16] -1.165479178 0.863160668 1.817002845 1.649951357 0.677040222
> (x<-z[z>0 & z<2])
[1] 0.04778818 0.67275779 1.08201500 0.52053095 0.81719413 0.79893598
[7] 0.58265652 0.86316067 1.81700284 1.64995136 0.67704022
> length(x); sum(x); mean(x)
[1] 11
[1] 9.529034
[1] 0.8662758
[3번 풀이]
> (x<-paste( rep(c("A","B","C"),c(6,6,6)), rep(rep(c(1,2),c(3,3)),3), sep=""))
[1] "A1" "A1" "A1" "A2" "A2" "A2" "B1" "B1" "B1" "B2" "B2" "B2" "C1" "C1"
[15] "C1" "C2" "C2" "C2"
> (y<-rep(c("a","b","c"),6))
[1] "a" "b" "c" "a" "b" "c" "a" "b" "c" "a" "b" "c" "a" "b" "c" "a" "b" "c"
> (z<-paste(x,y,sep="_"))
[1] "A1_a" "A1_b" "A1_c" "A2_a" "A2_b" "A2_c" "B1_a" "B1_b" "B1_c" "B2_a"
[11] "B2_b" "B2_c" "C1_a" "C1_b" "C1_c" "C2_a" "C2_b" "C2_c"
[4번 풀이]
> (x <- c(100,60,55,52,40,37))
[1] 100 60 55 52 40 37
> (names(x) <- c("서울", "부산", "대구", "인천", "광주", "대전"))
[1] "서울" "부산" "대구" "인천" "광주" "대전"
> x
서울 부산 대구 인천 광주 대전
100 60 55 52 40 37
[5번 풀이]
> (x <- matrix( letters[1:6], nrow=2, byrow=T))
[,1] [,2] [,3]
[1,] "a" "b" "c"
[2,] "d" "e" "f"
> (y <- matrix( LETTERS[1:6], ncol=3, nrow=3, byrow=F))
[,1] [,2] [,3]
[1,] "A" "D" "A"
[2,] "B" "E" "B"
[3,] "C" "F" "C"
> (z <- rbind(x,y))
[,1] [,2] [,3]
[1,] "a" "b" "c"
[2,] "d" "e" "f"
[3,] "A" "D" "A"
[4,] "B" "E" "B"
[5,] "C" "F" "C"
[6번 풀이]
> (이름 <- c("홍길동","박문수","심순애"))
[1] "홍길동" "박문수" "심순애"
> (성별 <- c("남","남","여"))
[1] "남" "남" "여"
> (나이 <- c(30,28,25))
[1] 30 28 25
> (키 <- c(180, 175, 165))
[1] 180 175 165
> (data <- data.frame(이름,성별,나이,키))
이름 성별 나이 키
1 홍길동 남 30 180
2 박문수 남 28 175
3 심순애 여 25 165
> names(data)
[1] "이름" "성별" "나이" "키"
> row.names(data)
[1] "1" "2" "3"
[7번 풀이]
> x <- odbcConnectExcel("C:\\Users\\leep\\Desktop\\임시.xls")
> data <- sqlFetch(x,"Sheet1")
> close(x)
> data
이름 성별 나이 키
1 홍길동 남 30 180
2 박문수 남 28 175
3 심순애 여 25 165
[8번 풀이]
> (year <- c("2000년", "2010년"))
[1] "2000년" "2010년"
> (quater <- paste(1:4,"분기",sep=""))
[1] "1분기" "2분기" "3분기" "4분기"
> (result <- c("생산량","판매액"))
[1] "생산량" "판매액"
> (data2000 <- matrix(c(100,120,110,125,1230,1550,1400,1675),nrow=4,dimnames=list(quater,result)))
생산량 판매액
1분기 100 1230
2분기 120 1550
3분기 110 1400
4분기 125 1675
> (data2010 <- matrix(c(260,280,300,310,3200,3550,4100,4250),nrow=4,dimnames=list(quater,result)))
생산량 판매액
1분기 260 3200
2분기 280 3550
3분기 300 4100
4분기 310 4250
> (data <- array(0,dim=c(4,2,2),dimnames=list(quater,result,year)))
> data[,,1] <- data2000
> data[,,2] <- data2010
> data
, , 2000년
생산량 판매액
1분기 100 1230
2분기 120 1550
3분기 110 1400
4분기 125 1675
, , 2010년
생산량 판매액
1분기 260 3200
2분기 280 3550
3분기 300 4100
4분기 310 4250
[9번 풀이]
> data[1,,]
2000년 2010년
생산량 100 260
판매액 1230 3200
> t(data[1,,])
생산량 판매액
2000년 100 1230
2010년 260 3200
[10번 풀이]
> x <- runif(20)
> y <- 1/x
> options(digits=4, width=40)
> y
[1] 8.203 2.567 1.241 3.051 1.036
[6] 2.603 1.052 3.738 1.332 5.221
[11] 2.820 1.752 1.112 1.588 2.069
[16] 2.054 12.763 1.682 2.053 10.546
[11번 풀이]
> birthdate <- c("1960/05/22", "1965/12/28", "1990/7/21")
> x <- as.Date(birthdate)
> diff(x)
Time differences in days
[1] 2046 8971
[12번 풀이]
> x <- month.name
> x
[1] "January" "February" "March" "April" "May" "June"
[7] "July" "August" "September" "October" "November" "December"
> grep("(ber)",x)
[1] 9 10 11 12
> x[grep("(ber)",x)]
[1] "September" "October" "November" "December"
[13번 풀이]
> x <- seq(from=132, to=184, by=0.32547);x
[1] 132.0000 132.3255 132.6509 132.9764 133.3019 133.6274 133.9528 134.2783
[9] 134.6038 134.9292 135.2547 135.5802 135.9056 136.2311 136.5566 136.8820
[17] 137.2075 137.5330 137.8585 138.1839 138.5094 138.8349 139.1603 139.4858
[25] 139.8113 140.1368 140.4622 140.7877 141.1132 141.4386 141.7641 142.0896
[33] 142.4150 142.7405 143.0660 143.3914 143.7169 144.0424 144.3679 144.6933
[41] 145.0188 145.3443 145.6697 145.9952 146.3207 146.6462 146.9716 147.2971
[49] 147.6226 147.9480 148.2735 148.5990 148.9244 149.2499 149.5754 149.9008
[57] 150.2263 150.5518 150.8773 151.2027 151.5282 151.8537 152.1791 152.5046
[65] 152.8301 153.1556 153.4810 153.8065 154.1320 154.4574 154.7829 155.1084
[73] 155.4338 155.7593 156.0848 156.4102 156.7357 157.0612 157.3867 157.7121
[81] 158.0376 158.3631 158.6885 159.0140 159.3395 159.6650 159.9904 160.3159
[89] 160.6414 160.9668 161.2923 161.6178 161.9432 162.2687 162.5942 162.9196
[97] 163.2451 163.5706 163.8961 164.2215 164.5470 164.8725 165.1979 165.5234
[105] 165.8489 166.1744 166.4998 166.8253 167.1508 167.4762 167.8017 168.1272
[113] 168.4526 168.7781 169.1036 169.4290 169.7545 170.0800 170.4055 170.7309
[121] 171.0564 171.3819 171.7073 172.0328 172.3583 172.6838 173.0092 173.3347
[129] 173.6602 173.9856 174.3111 174.6366 174.9620 175.2875 175.6130 175.9384
[137] 176.2639 176.5894 176.9149 177.2403 177.5658 177.8913 178.2167 178.5422
[145] 178.8677 179.1932 179.5186 179.8441 180.1696 180.4950 180.8205 181.1460
[153] 181.4714 181.7969 182.1224 182.4478 182.7733 183.0988 183.4243 183.7497
> y <- (145<x & x<164);y
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[49] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[73] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[85] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[97] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[157] FALSE FALSE FALSE FALSE
> sum(y)
[1] 59
[14번 풀이]
> 생산량1 <- seq(from=1230, by=10, length=24); 생산량1
[1] 1230 1240 1250 1260 1270 1280 1290 1300 1310 1320 1330 1340 1350 1360 1370
[16] 1380 1390 1400 1410 1420 1430 1440 1450 1460
> 생산량 <- rep(생산량1, each=2); 생산량
[1] 1230 1230 1240 1240 1250 1250 1260 1260 1270 1270 1280 1280 1290 1290 1300
[16] 1300 1310 1310 1320 1320 1330 1330 1340 1340 1350 1350 1360 1360 1370 1370
[31] 1380 1380 1390 1390 1400 1400 1410 1410 1420 1420 1430 1430 1440 1440 1450
[46] 1450 1460 1460
> 수익률1 <- seq(from=0.225, by=0.002, length=16);수익률1
[1] 0.225 0.227 0.229 0.231 0.233 0.235 0.237 0.239 0.241 0.243 0.245 0.247
[13] 0.249 0.251 0.253 0.255
> 수익률 <- rep(수익률1, each=3);수익률
[1] 0.225 0.225 0.225 0.227 0.227 0.227 0.229 0.229 0.229 0.231 0.231 0.231
[13] 0.233 0.233 0.233 0.235 0.235 0.235 0.237 0.237 0.237 0.239 0.239 0.239
[25] 0.241 0.241 0.241 0.243 0.243 0.243 0.245 0.245 0.245 0.247 0.247 0.247
[37] 0.249 0.249 0.249 0.251 0.251 0.251 0.253 0.253 0.253 0.255 0.255 0.255
> 자료 <- cbind(생산량, 수익률)
> 시계열자료 <- ts(data=자료, start=c(2000,4), end=c(2014,3), frequency=12)
> 시계열자료
생산량 수익률
Apr 2010 1230 0.225
May 2010 1230 0.225
Jun 2010 1240 0.225
Jul 2010 1240 0.227
Aug 2010 1250 0.227
Sep 2010 1250 0.227
Oct 2010 1260 0.229
Nov 2010 1260 0.229
Dec 2010 1270 0.229
Jan 2011 1270 0.231
Feb 2011 1280 0.231
Mar 2011 1280 0.231
Apr 2011 1290 0.233
May 2011 1290 0.233
Jun 2011 1300 0.233
Jul 2011 1300 0.235
Aug 2011 1310 0.235
Sep 2011 1310 0.235
Oct 2011 1320 0.237
Nov 2011 1320 0.237
Dec 2011 1330 0.237
Jan 2012 1330 0.239
Feb 2012 1340 0.239
Mar 2012 1340 0.239
Apr 2012 1350 0.241
May 2012 1350 0.241
Jun 2012 1360 0.241
Jul 2012 1360 0.243
Aug 2012 1370 0.243
Sep 2012 1370 0.243
Oct 2012 1380 0.245
Nov 2012 1380 0.245
Dec 2012 1390 0.245
Jan 2013 1390 0.247
Feb 2013 1400 0.247
Mar 2013 1400 0.247
Apr 2013 1410 0.249
May 2013 1410 0.249
Jun 2013 1420 0.249
Jul 2013 1420 0.251
Aug 2013 1430 0.251
Sep 2013 1430 0.251
Oct 2013 1440 0.253
Nov 2013 1440 0.253
Dec 2013 1450 0.253
Jan 2014 1450 0.255
Feb 2014 1460 0.255
Mar 2014 1460 0.255
[15번 풀이]
> LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
> X <- sample(LETTERS[1:20],10)
> Y <- sample(LETTERS[7:26],10)
> sort(X); sort(Y)
[1] "A" "B" "F" "H" "I" "L" "O" "P" "S" "T"
[1] "M" "O" "P" "R" "S" "T" "U" "V" "W" "Y"
> sort(intersect(X,Y))
[1] "O" "P" "S" "T"
> sort(setdiff(X,Y))
[1] "A" "B" "F" "H" "I" "L"