#DASAR-DASAR R

#VECTOR
a1 <- c(2,3,4,5)
a1
## [1] 2 3 4 5
a2 <- seq (2,10,by=2)
a2
## [1]  2  4  6  8 10
a3 <- seq (2,10,length.out=10)
a3
##  [1]  2.000000  2.888889  3.777778  4.666667  5.555556  6.444444  7.333333
##  [8]  8.222222  9.111111 10.000000
a4 <- rep (1,5)
a4
## [1] 1 1 1 1 1
a5 <- rep (1:9,2)
a5
##  [1] 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
a6 <- rep (3:6,3:6)
a6
##  [1] 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6
a7 <- rep (3:6,each=2)
a7
## [1] 3 3 4 4 5 5 6 6
a8 <- paste("A","B",a1,sep="")
a8
## [1] "AB2" "AB3" "AB4" "AB5"
a9 <- paste ("A","B",a1)
a9
## [1] "A B 2" "A B 3" "A B 4" "A B 5"
a10 <- paste0("A","B",a1)
a10
## [1] "AB2" "AB3" "AB4" "AB5"
#MATRIKS

b1 <- 1:16
b1
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
b11 <- matrix(b1,4,4)
b11
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
b2 <- matrix (b1,4,5)
## Warning in matrix(b1, 4, 5): data length [16] is not a sub-multiple or multiple
## of the number of columns [5]
b2
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    5    9   13    1
## [2,]    2    6   10   14    2
## [3,]    3    7   11   15    3
## [4,]    4    8   12   16    4
b3 <- matrix (b1,4,5,byrow = TRUE)
## Warning in matrix(b1, 4, 5, byrow = TRUE): data length [16] is not a
## sub-multiple or multiple of the number of columns [5]
b3
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    6    7    8    9   10
## [3,]   11   12   13   14   15
## [4,]   16    1    2    3    4
b4 <- matrix (c(2,4,6,8,10,-2,-4,-6,-8),2,3)
## Warning in matrix(c(2, 4, 6, 8, 10, -2, -4, -6, -8), 2, 3): data length [9] is
## not a sub-multiple or multiple of the number of rows [2]
b4
##      [,1] [,2] [,3]
## [1,]    2    6   10
## [2,]    4    8   -2
dim (b1) <- c(4,4)
b1
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
b5 <- matrix (2:6,3)
## Warning in matrix(2:6, 3): data length [5] is not a sub-multiple or multiple of
## the number of rows [3]
b5
##      [,1] [,2]
## [1,]    2    5
## [2,]    3    6
## [3,]    4    2
b6 <- matrix (7:10,3)
## Warning in matrix(7:10, 3): data length [4] is not a sub-multiple or multiple
## of the number of rows [3]
b6
##      [,1] [,2]
## [1,]    7   10
## [2,]    8    7
## [3,]    9    8
rbind (b5,b6)
##      [,1] [,2]
## [1,]    2    5
## [2,]    3    6
## [3,]    4    2
## [4,]    7   10
## [5,]    8    7
## [6,]    9    8
cbind (b5,b6)
##      [,1] [,2] [,3] [,4]
## [1,]    2    5    7   10
## [2,]    3    6    8    7
## [3,]    4    2    9    8
nrow (b2)
## [1] 4
ncol (b4)
## [1] 3
rowSums (b11)
## [1] 28 32 36 40
colSums(b11)
## [1] 10 26 42 58
b8 <- t (b11)
b8
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
## [4,]   13   14   15   16
#ARRAY

c1 <- 2:20
c1
##  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
c2 <- array (c1,dim=c(2,2,3))
c2
## , , 1
## 
##      [,1] [,2]
## [1,]    2    4
## [2,]    3    5
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    6    8
## [2,]    7    9
## 
## , , 3
## 
##      [,1] [,2]
## [1,]   10   12
## [2,]   11   13
c3 <- array (c1,dim=c(2,2,3,2))
c3
## , , 1, 1
## 
##      [,1] [,2]
## [1,]    2    4
## [2,]    3    5
## 
## , , 2, 1
## 
##      [,1] [,2]
## [1,]    6    8
## [2,]    7    9
## 
## , , 3, 1
## 
##      [,1] [,2]
## [1,]   10   12
## [2,]   11   13
## 
## , , 1, 2
## 
##      [,1] [,2]
## [1,]   14   16
## [2,]   15   17
## 
## , , 2, 2
## 
##      [,1] [,2]
## [1,]   18   20
## [2,]   19    2
## 
## , , 3, 2
## 
##      [,1] [,2]
## [1,]    3    5
## [2,]    4    6
c4 <- array (1:5,dim=c(2,2,2,2))
c4
## , , 1, 1
## 
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## , , 2, 1
## 
##      [,1] [,2]
## [1,]    5    2
## [2,]    1    3
## 
## , , 1, 2
## 
##      [,1] [,2]
## [1,]    4    1
## [2,]    5    2
## 
## , , 2, 2
## 
##      [,1] [,2]
## [1,]    3    5
## [2,]    4    1
#FACTOR

d1 <- c ("A","B","AB","O")
d1
## [1] "A"  "B"  "AB" "O"
d2 <- factor (d1)
d2
## [1] A  B  AB O 
## Levels: A AB B O
d3 <- factor (d1,levels= c("0","AB","A","B"))
d3
## [1] A    B    AB   <NA>
## Levels: 0 AB A B
d4 <- c("S1","D4","D3")
d4
## [1] "S1" "D4" "D3"
d5 <- ordered (d4)
d5
## [1] S1 D4 D3
## Levels: D3 < D4 < S1
d6 <- ordered (d4,levels= d4)
d6
## [1] S1 D4 D3
## Levels: S1 < D4 < D3
nama_bunga <- data.frame (bunga= c("melati","tulip","sepatu","matahari","mawar","kembang"),warna =c("ungu","putih","rainbow","kuning","merah","ping"),jumlah=c(5,4,3,6,7,2))
nama_bunga
write.table (nama_bunga, file="C:/Users/HP/OneDrive/KRS SEMESTER 3/nama_bunga.txt",row.names=FALSE,sep=";")
library(openxlsx)
write.xlsx (nama_bunga, file="C:/Users/HP/OneDrive/KRS SEMESTER 3/nama_bunga.xlsx",rowNames=FALSE)
Bunga <- read.csv("C:/Users/HP/OneDrive/KRS SEMESTER 3/nama_bunga2.csv",header=TRUE,sep=";")
Bunga
Bunga$baru <- c(5,5,5,5,5,5)
Bunga
Bunga$stok <- Bunga$jumlah + Bunga$baru
Bunga
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
Bunga2 <- Bunga %>% select(-warna)
Bunga2
Bunga$kategori [Bunga$stok > 10] <- "banyak"
Bunga$kategori [Bunga$stok <= 10 ] <- "sedikit"
Bunga
Bunga3 <- read.csv ("C:/Users/HP/OneDrive/KRS SEMESTER 3/nama_bunga2.csv",header=TRUE,sep=";",stringsAsFactors = TRUE)
Bunga3
summary(Bunga3)
##       bunga       warna       jumlah    
##  kembang :1   kuning :1   Min.   :2.00  
##  matahari:1   merah  :1   1st Qu.:3.25  
##  mawar   :1   ping   :1   Median :4.50  
##  melati  :1   putih  :1   Mean   :4.50  
##  sepatu  :1   rainbow:1   3rd Qu.:5.75  
##  tulip   :1   ungu   :1   Max.   :7.00
summary(Bunga3$warna)
##  kuning   merah    ping   putih rainbow    ungu 
##       1       1       1       1       1       1
summary(Bunga3$jumlah)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2.00    3.25    4.50    4.50    5.75    7.00
bilangan <- c(1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1,1,3,5,7,9)
table(bilangan)
## bilangan
## 1 2 3 4 5 6 7 8 9 
## 3 2 3 2 3 2 3 2 3
jawaban <- c('ya','ya','tidak','ya','tidak','ya')
table(jawaban)
## jawaban
## tidak    ya 
##     2     4
bilangan <- c(1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1,1,3,5,7,9)
distbil <- cut(bilangan,breaks = c(1,4,6,9),include.lowest = TRUE,right = FALSE)
table(distbil)
## distbil
## [1,4) [4,6) [6,9] 
##     8     5    10
tinggi <- c(
176 ,167, 180, 165, 168, 171, 177, 176, 170, 175,
169, 171, 171, 176, 166, 179, 181, 174 ,167, 172,
170, 169, 175, 178, 171, 168, 178, 183, 174, 166,
181, 172, 177, 182, 167, 179, 183, 185, 185, 173, 
179, 180, 184, 170, 174, 175, 176, 175, 182, 172)


jumlahK <- round(1+(3.322*log10(length(tinggi))))
jumlahK
## [1] 7
Range <- max(tinggi) - min(tinggi)
Range
## [1] 20
h <- Range/jumlahK
round(h)
## [1] 3
max(tinggi);min(tinggi)
## [1] 185
## [1] 165
library(fdth)
## 
## Attaching package: 'fdth'
## The following objects are masked from 'package:stats':
## 
##     sd, var
dist_tinggi <- fdt (tinggi,start=164.5,end=185.5,h=3)
dist_tinggi
##   Class limits  f   rf rf(%) cf cf(%)
##  [164.5,167.5)  6 0.12    12  6    12
##  [167.5,170.5)  7 0.14    14 13    26
##  [170.5,173.5)  8 0.16    16 21    42
##  [173.5,176.5) 11 0.22    22 32    64
##  [176.5,179.5)  7 0.14    14 39    78
##  [179.5,182.5)  6 0.12    12 45    90
##  [182.5,185.5)  5 0.10    10 50   100
bunga <- c("melati","tulip","sepatu","matahari","mawar","kembang")
pemilik <- c("jana","hima","sara","inda","maga","resa")
punya <- data.frame (bunga,pemilik)
table (punya)
##           pemilik
## bunga      hima inda jana maga resa sara
##   kembang     0    0    0    0    1    0
##   matahari    0    1    0    0    0    0
##   mawar       0    0    0    1    0    0
##   melati      0    0    1    0    0    0
##   sepatu      0    0    0    0    0    1
##   tulip       1    0    0    0    0    0