You are on page 1of 15

KELOMPOK 5

Part VI : The Part Of Tens

SYIFA RIFDA SAYYIDAH(1902536) HARIS RIZAL HIMAWAN (1907824)


SILVY NURAPIPAH (1902476) HANA MALIHAH ARDIANTI (1902505)

LUTHFI JULIANNIKA N (1902419) NONAME

NONAME NONAME
Chapter 19 : Ten Things You Can Do
In R That You Would’ve Done In
Microsoft Excell

Daftar Isi

Chapter 20 : Ten Tips On Working


With Packaages
Chapter 19 : Ten Things You Can
Do In R That You Would’ve Done
In Microsoft Excell
Adding Row and Column
Totals
> iris.num <- iris[, -5]
> colSums(iris.num)
> colMeans(iris.num)
> sapply(iris.num, min)
> sapply(iris.num, max)
Formatting Numbers
> format(12345.6789, digits = 9, > set.seed(1)
decimal.mark = ",",
> x <- 1000 * runif(5)
+ big.mark = " ", small.mark =
".", small.interval = 3) > sprintf("$ %3.2f", x)

[1] "12 345,678.9"> x <- [1] "$ 265.51" "$ 372.12" "$ 572.85"
colMeans(mtcars[, 1:4]) "$ 908.21" "$ 201.68"

> format(x, digits = 2, nsmall = > stuff <- c("bread", "cookies")


2)
> price <- c(2.1, 4)
mpg cyl disp hp
> sprintf("%s cost $ %3.2f ", stuff,
" 20.09" " 6.19" "230.72" price)
"146.69"
[1] "bread cost $ 2.10 " "cookies
> x <- seq(0.5, 0.55, 0.01) cost $ 4.00 "

> sprintf("%.1f %%", 100 * x)

[1] "50.0 %" "51.0 %" "52.0 %"


"53.0 %" "54.0 %" "55.0 %"
Sorting Data
> with(mtcars, mtcars[order(hp), ])
> with(mtcars, mtcars[order(hp, decreasing = TRUE), ])
Making Choices with If
> mtcars <- transform(mtcars,

+ mpgClass = ifelse(mpg < mean(mpg),


"Low", "High"))

> mtcars[mtcars$mpgClass == "High", ]


Calculating
Conditional Totals
> with(mtcars, mean(mpg))
[1] 20.09062
> with(mtcars, mean(mpg[hp < 150]))
[1] 24.22353
> with(mtcars, mean(mpg[hp >= 150]))
[1] 15.40667
> with(mtcars, length(mpg[hp > 150]))
[1] 13
Transposing Columns or Rows
> x <- matrix(1:12, ncol = 3)

>x

[,1] [,2] [,3]

[1,] 1 5 9 > t(mtcars[1:4, ])


Mazda RX4 Mazda RX4 Wag Datsun
[2,] 2 6 10
710 Hornet 4 Drive
[3,] 3 7 11 mpg "21.0" "21.0" "22.8" "21.4"
cyl "6" "6" "4" "6"
[4,] 4 8 12 disp "160" "160" "108" "258"
hp "110" "110" " 93" "110"
> t(x) drat "3.90" "3.90" "3.85" "3.08"
wt "2.620" "2.875" "2.320" "3.215"
[,1] [,2] [,3] [,4] qsec "16.46" "17.02" "18.61" "19.44"
vs "0" "0" "1" "1"
[1,] 1 2 3 4 am "1" "1" "1" "0"
[2,] 5 6 7 8 gear "4" "4" "4" "3"
carb "4" "4" "1" "1"
[3,] 9 10 11 12 mpgClass "High" "High" "High" "High"
Finding Unique or Duplicated
Values
> unique(mtcars$cyl)
[1] 6 4 8
> dupes <- duplicated(iris)
> head(dupes)
[1] FALSE FALSE FALSE FALSE FALSE FALSE
> which(dupes)
[1] 143
> iris[dupes, ]
Sepal.Length Sepal.Width Petal.Length Petal.Width
Species
143 5.8 2.7 5.1 1.9 virginica

> iris[!dupes, ]
> nrow(iris[!dupes, ])
[1] 149
Working with Lookup Tables

> index <- match("Toyota Corolla",


rownames(mtcars))
> index
[1] 20
> mtcars[index, 1:4]
mpg cyl disp hp
Toyota Corolla 33.9 4 71.1 65
Working with Pivot Tables
> with(mtcars, tapply(hp, list(cyl, gear),
mean))
345
4 97.0000 76.0 102.0
6 107.5000 116.5 175.0
8 194.1667 NA 299.5
cyl gear am hp
1 4 3 0 97.00000
2 6 3 0 107.50000
3 8 3 0 194.16667
4 4 4 0 78.50000
5 6 4 0 123.00000
6 4 4 1 75.16667
7 6 4 1 110.00000
8 4 5 1 102.00000
9 6 5 1 175.00000
10 8 5 1 299.50000
Using the Goal Seek
and Solver
> optimize(revenue, interval =
c(50, 150), maximum = TRUE)
$maximum
[1] 100
$objective
[1] 5000
Chapter 20 :Ten Tips on Working
with Packages
1. Poking Around the Nooks and
Crannies of CRAN
2. Finding Interesting Packages
3. Installing Packages
4. Loading Packages
5. Reading the Package Manual and
Vignette
6. Updating Packages
7. Forging Ahead with R‐Forge
8. Getting packages from github
9. Conducting Installations from
BioConductor

You might also like