R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

x <- c(10, 20, 30, 40)
x
## [1] 10 20 30 40
ls()
## [1] "x"

bab II -vektor dan aritmetika

nilai <- c(85, 90, 76, 88, 95)
mean(nilai)
## [1] 86.8
sd(nilai)
## [1] 7.049823
sum(nilai) / length(nilai)
## [1] 86.8

-generate sequence

1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(0, 1, by = 0.1)
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
rep(c(1,2,3), times = 2)
## [1] 1 2 3 1 2 3

-logical vector

lulus <- nilai >= 80
lulus
## [1]  TRUE  TRUE FALSE  TRUE  TRUE

-missing value

data_na <- c(23, NA, 45, NA, 12)
is.na(data_na)
## [1] FALSE  TRUE FALSE  TRUE FALSE
mean(data_na, na.rm = TRUE)
## [1] 26.66667

-character vector

nama <- c("sasa", "firna", "nena")
paste(nama, "lulus")
## [1] "sasa lulus"  "firna lulus" "nena lulus"

-indexing/subsetting

nilai[lulus]          
## [1] 85 90 88 95
nilai[nilai < 80]     
## [1] 76
nilai[-1]             
## [1] 90 76 88 95

BAB III -objek, mode dan atribut mode(nilai) => numeric mode(nama) => character mode(lulus) => logical length(nilai)

-mengubah panjang objek

length(nilai) <- 3
nilai
## [1] 85 90 76
class(nilai)
## [1] "numeric"
attributes(nilai)
## NULL

BAB IV - FACTOR (DATA KATEGORIK)

jk <- factor(c("L", "P", "L", "P", "P"))
levels(jk)
## [1] "L" "P"
table(jk)
## jk
## L P 
## 2 3

-RATA RATA NILAI PER KELOMPOK JENIS KELAMIN

nilai2 <- c(85, 78, 90, 88, 76)
tapply(nilai2, jk, mean)
##        L        P 
## 87.50000 80.66667

-ORDERED FACTOR

tingkat <- factor(c("Rendah","Tinggi","Sedang"),
                   levels = c("Rendah","Sedang","Tinggi"),
                   ordered = TRUE)
tingkat
## [1] Rendah Tinggi Sedang
## Levels: Rendah < Sedang < Tinggi
tingkat[1] < tingkat[2]
## [1] TRUE

BAB V (ARRAY DAN MATRIKS)

m <- matrix(1:9, nrow = 3, ncol = 3)
m
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
t(m)              #transpose
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
dim(m)
## [1] 3 3
A <- matrix(c(2,1,1,3), nrow = 2)
B <- matrix(c(1,0,0,1), nrow = 2)
A %*% B           # perkalian matriks (bukan A*B!)
##      [,1] [,2]
## [1,]    2    1
## [2,]    1    3
solve(A)          # invers matriks
##      [,1] [,2]
## [1,]  0.6 -0.2
## [2,] -0.2  0.4
diag(A)
## [1] 2 3

-cbind/rbind

  cbind(m, c(10,11,12))
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
rbind(m, c(10,11,12))
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## [4,]   10   11   12