Nama: Asnur Saputra NIM: F1A220034 Kelas: B Prodi: S1 Statistika

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Nama : Asnur Saputra

NIM : F1A220034
Kelas :B
Prodi : S1 Statistika

Soal:
Kerjakanlah menggunakan aplikasi R contoh-contoh yang terdapat pada materi
BAB 1 dan BAB 2.
Jawab:
Contoh pada BAB 1: Menginstall packages spuRs
> 1+5
[1] 6
> x<-3+3
> x
[1] 6

Soal pada BAB 2:


Aritmatika:
> (1+8/2)^30
[1] 9.313226e+20
> 18%%2
[1] 0
> 18%%7
[1] 4
> 18%/%7
[1] 2
> exp(2)
[1] 7.389056
> pi
[1] 3.141593
> cos(pi/3)
[1] 0.5

Variabel:
> x<-100
> 10^x
[1] 1e+100
> 5*x
[1] 500
> (y<-(1+100/x)^2)
[1] 4
> n<-2
> n<-n+3
> n
[1] 5

Functions:
> seq(from=2,to=12,by=3)
[1] 2 5 8 11
> seq(from=13,to=20)
[1] 13 14 15 16 17 18 19 20
> seq(1,9,2)
[1] 1 3 5 7 9
> seq(by=0,10,2)
Error in seq.default(by = 0, 10, 2) : invalid '(to - from)/by'
> seq(by=-2,9,1)
[1] 9 7 5 3 1
> x<-9
> seq(2,x,x/9)
[1] 2 3 4 5 6 7 8 9

Vector:
> (x<-seq(2,10,by=1))
[1] 2 3 4 5 6 7 8 9 10
> (y<-rep(1,5))
[1] 1 1 1 1 1
> (z<-c(x,y))
[1] 2 3 4 5 6 7 8 9 10 1 1 1 1 1
> (x<-88:94)
[1] 88 89 90 91 92 93 94
> i<-c(2,4,5)
> x[i]
[1] 89 91 92
> j<-c(-1,-2,-3)
> x[j]
[1] 91 92 93 94
> x<-c()
> length(x)
[1] 0
> x<-c(2,4,7)
> y<-(10,9,7)
Error: unexpected ',' in "y<-(10,"
> y<-(10,9,7)
Error: unexpected ',' in "y<-(10,"
> y<-c(10,9,7)
> x*y
[1] 20 36 49
> x+y
[1] 12 13 14
> y^x
[1] 100 6561 823543
> c(2,7,1,9)+c(2,5)
[1] 4 12 3 14
> (2:6)^c(2,3)
[1] 4 27 16 125 36
Warning message:
In (2:6)^c(2, 3) :
longer object length is not a multiple of shorter object
length
> (2:7)^c(1,2)
[1] 2 9 4 25 6 49
> 2+c(1,3,2)
[1] 3 5 4
> 2*(1,4,6,8)
Error: unexpected ',' in "2*(1,"
> 2*c(1,4,6,8)
[1] 2 8 12 16
> (2:11)^3
[1] 8 27 64 125 216 343 512 729 1000 1331
> sqrt(1:5)
[1] 1.000000 1.414214 1.732051 2.000000 2.236068
> mean(2:20)
[1] 11
> sort(c(1,4,7,10,23))
[1] 1 4 7 10 23
> x<-c(2.3,0.1,2.1,2,0.4)
> x.mean<-sum(x)/length(x)
> x.mean
[1] 1.38
> x.var<-sum((x-x.mean)^2)/(length(x)-1)
> x.var
[1] 1.087
> dt<-0.005
> t<-seq(0,pi/6,by=dt)
> ft<-cos(t)
> (i<-sum(ft)*dt)
[1] 0.5015487
> i-cos(pi/3)
[1] 0.001548651
> x<-seq(10,200,by=10)
> y<-(1+1/x)^x
> exp(1)-y
[1] 0.124539368 0.064984123 0.043963053 0.033217990 0.026693799
0.022311689
[7] 0.019165457 0.016796888 0.014949367 0.013467999 0.012253747
0.011240338
[13] 0.010381747 0.009645015 0.009005917 0.008446252 0.007952077
0.007512533
[19] 0.007119034 0.006764706
> plot(x,y)
Missing data:
> a<-NA
> is.na(a)
[1] TRUE
> a<-c(12,NA,17)
> is.na(a)
[1] FALSE TRUE FALSE
> mean(a)
[1] NA
> mean(a,na.rm=TRUE)
[1] 14.5

Expressions and assignments:


> seq(11,21,by=2)
[1] 11 13 15 17 19 21
> 3
[1] 3
> mean(c(1,4,6)
+
+ s
Error: unexpected symbol in:
"
s"
> mean(c(1,4,5))
[1] 3.333333
> mean(c(1,3,5,7)
+ j
Error: unexpected symbol in:
"mean(c(1,3,5,7)
j"
> mean(c(1,3,5,7))
[1] 4
> 2<4
[1] TRUE
> x1<-seq(11,21,by=2)
> x2<-3
> x3<-mean(c(1,4,5))
> x4<-1<2
> x1
[1] 11 13 15 17 19 21
> x2
[1] 3
> x3
[1] 3.333333
> x4
[1] TRUE

Logical expressions:
> c(0,0,1,1)|c(0,1,1,0)
[1] FALSE TRUE TRUE TRUE
> xor(c(0,0,1,1),c(0,0,0,0
+ e
Error: unexpected symbol in:
"xor(c(0,0,1,1),c(0,0,0,0
e"
> xor(c(0,0,1,1),c(0,0,0,0)
+ k
Error: unexpected symbol in:
"xor(c(0,0,1,1),c(0,0,0,0)
k"
> xor(c(0,0,1,1),c(0,0,0,0))
[1] FALSE FALSE TRUE TRUE
> x<-2:20
> x%%5==0
[1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
FALSE FALSE
[13] FALSE TRUE FALSE FALSE FALSE FALSE TRUE
> (y<-c(1,NA,3,4)
+ EE
Error: unexpected symbol in:
"(y<-c(1,NA,3,4)
EE"
> (y<-c(1,NA,3,4)
+ DF
Error: unexpected symbol in:
"(y<-c(1,NA,3,4)
DF"
> (Y<-C(1,NA,3,4))
Error in C(1, NA, 3, 4) : object not interpretable as a factor
> (Y<-c(1,NA,3,4))
[1] 1 NA 3 4
> x<-c(1,NA,3,4)
> x>3
[1] FALSE NA FALSE TRUE
> x[x>4]
[1] NA
> subset(x,subset=x>3)
[1] 4
> x<-c(1,1,2,3,5,8,13)
> which(x%%2==0)
[1] 3 6
> 2*3==1
[1] FALSE
> sqrt(3)*sqrt(2)==2
[1] FALSE
> all.equal(sqrt(3)*sqrt(2),2)
[1] "Mean relative difference: 0.1835034"
> x<-1
> x*sin(x)==0
[1] FALSE
> x<-0
> x*sin(x)==0
[1] TRUE
> (x==0)|(sin(1/x)==0)
[1] TRUE
Warning message:
In sin(1/x) : NaNs produced
> (x==0)||(sin(1/x)==0)
[1] TRUE

Matriks:
> (a<-matrix(2:10,nrow=3,ncol=3,byrow=FALSE))
[,1] [,2] [,3]
[1,] 2 5 8
[2,] 3 6 9
[3,] 4 7 10
> (a<-matrix(2:10,nrow=3,ncol=3,byrow=TRUE))
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 5 6 7
[3,] 8 9 10
> a[1,3]<-0
> a[,2:3]
[,1] [,2]
[1,] 3 0
[2,] 6 7
[3,] 9 10
> (b<-diag(c(1,1,1)))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
> (x<-matrix(c(3,7,6,5),nrow=2,ncol=2))
[,1] [,2]
[1,] 3 6
[2,] 7 5
> (y<-matrix(c(1,3,2,6),nrow=2,ncol=2))
[,1] [,2]
[1,] 1 2
[2,] 3 6
> x%%y
[,1] [,2]
[1,] 0 0
[2,] 1 5
> x*y
[,1] [,2]
[1,] 3 12
[2,] 21 30
> (x.inv<-solve(x))
[,1] [,2]
[1,] -0.1851852 0.2222222
[2,] 0.2592593 -0.1111111
> x%*%x.inv
[,1] [,2]
[1,] 1 1.110223e-16
[2,] 0 1.000000e+00
> x^(-1)
[,1] [,2]
[1,] 0.3333333 0.1666667
[2,] 0.1428571 0.2000000
> x[3]
[1] 6

You might also like