Professional Documents
Culture Documents
REVISION (QUIZ 1) - R
BASIC OPERATIONS
To combine variables in a Contoh <- c(3,4,5,6) [1] 3 4 5 6
vector Contoh
Contoh[2] [1] 4
Contoh[0] Numeric(0)
Read the table from csv file Namabaru <- read.csv(file=”simple.csv”, *shows the table*
head=TRUE, sep= “,”);
Namabaru
$class
[1] "data.frame"
$row.names
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26
[27] 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 51 52
[53] 53 54
VECTORS
To make assignment of a single number A <- 3 [1] 3
A
Can do basic operations juga B <- sqrt(a*a+3) [1] 3.456784
B
To list the variables you have defined in a Ls() [1] “A” “b”
particular session
To create a vector A <- c(1,2,4,5) [1] 1 2 4 5
A
To do basic operations with the vector A+1 [1] 2 3 5 6
Mean(A) [1] 3
Var(a) [1] 2.5
To access to particular entries in the A[1] [1] 1
vector A[7] [1] NA
To create a list of 10 numbers, initialized A <- numeric(10) [1] 0 0 0 0 0 0 0 0 0 0
to zero A
To determine data type Typeof(a) [1] “double”
For strings A <- c(“hello”,”there”) [1] “hello” “there”
A [1] “character”
Typeof(a)
R can store data as a factor. summary(tree$CHBR) A1 A2 A3 A4 A5 A6 A7 B1 B2 B3 B4 B5
B6 B7 C1 C2 C3
3 1 1 3 1 3 1 1 3 3 3 3 3 3 1 3
1 3 1 1
[1] 21
MATRIX
To make a matrix matrix_a <- matrix(1:10, byrow = TRUE, nrow = 5) > matrix_a
[,1] [,2]
matrix_a [1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
[5,] 9 10
LIST
To create a list vect <- 1:5 > vect
[1] 1 2 3 4 5
/// atau
Vect <- c(1:5) > mat
vect [,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 1
mat <- matrix(1:9, ncol =5)
mat //note that bila tak letak byrow = TRUE, dia akan susun
dim(mat) kebawah dulu bukan kekanan dulu.
> mylist
[[1]] //note ada double bracket here
[1] 1 2 3 4 5
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 1
[[3]]
DAX SMI CAC FTSE
[1,] 1628.75 1678.1 1772.8 2443.6
[2,] 1613.63 1688.5 1750.5 2460.2
[3,] 1606.51 1678.6 1718.0 2448.2
[4,] 1621.04 1684.1 1708.1 2470.4
[5,] 1618.16 1686.6 1723.1 2484.7
[6,] 1610.61 1671.6 1714.3 2466.8
[7,] 1630.75 1682.9 1734.5 2487.9
[8,] 1640.17 1703.6 1757.4 2508.4
[9,] 1635.47 1697.5 1754.0 2510.5
[10,] 1645.89 1716.3 1754.3 2497.4
EXTERNAL
Baca data yang download from the PATH <-'https://raw.githubusercontent.com/guru99-edu/R- //banyak
internet Programming/master/prison.csv'
df <- read.csv(PATH)[1:5]
1. NORMAL DISTRIBUTION
// If you only give the points it assumes you want to use a mean of zero and standard deviation of one.
> dnorm(0)
[1] 0.3989423
> dnorm(0)*sqrt(2*pi)
[1] 1
> dnorm(0,mean=4)
[1] 0.0001338302
> dnorm(0,mean=4,sd=10)
[1] 0.03682701
1. golub = matrix
2. golub[1,1] = numeric
3. golub.cl = numeric
4. golub.gnames = matrix
5. apply = function
6. exp = function
7. gol.fac = factor
8. plot = StandardGeneric
9. ALL = ExpressionSet
(b) What is the meaning of the following abbreviations: rm, sum, prod, seq, sd, nrow.
Answer:
1. rm = rm command is used to remove files or directory.
2. Sum = sum command is summation. It is used to calculate the sum of vector elements.
3. Prod = prod is the product which shows the multiplication results of all the values present in its arguments.
4. Seq = seq command is used to create a sequence of numbers.
5. Sd = sd stands for standard deviation.
6. Nrow = nrows is used to show the number of rows.
(c) For what purpose are the following functions useful: grep, apply, gl, library, source, setwd, history, str.
Answer:
1. grep = grep is useful in searching plain-text data sets for lines that match a regular expression.
2. Apply = apply is used to return a vector from a function on the rows or columns of a matrix.
3. gl = gl is used to create a factor by specifying the pattern of levels.
4. library = library is the location where the packages are stored and it is used to load add on packages.
5. source = source is used to accept its input from the named file or URL or connection or expressions directly.
6. setwd = setwd is used to set or change R working directory.
7. history = history is useful in printing the last commands given from the command line.
8. str = str is used as compact way to display the structure of an R object.
2. gendat Consider the data in the matrix gendat, constructed in Section 1.6. Its small size has the advantage that you can check your
computations even by a pocket calculator.
(a) Use apply to compute the standard deviation of the persons (column).
Answer:
apply(gendat,2,sd)
(b) Use apply to compute the standard deviation of the genes(row).
Answer: apply(gendat,1,sd)
(c) Order the matrix according to the gene standard deviations(row).
Answer:
sdgenesvalues <- apply(gendat,1,sd) //assign a variable first to count sd
o <- order(sdgenesvalues,decreasing=TRUE) //assign the order according to the ds
gendat[o,] //call the order, order the rows, all columns
STRIPCHARTS
A strip chart is the most basic type of plot available.
HISTOGRAM
It plots the frequencies that data appears within certain ranges.
SCATTERPLOTS
A scatter plot provides a graphical view of the relationship between two sets of numbers.
NORMAL QQ PLOTS
This plot is used to determine if your data is close to being normally distributed.
Create plot qqnorm(w1$vals, main="Normal Q-Q Plot of the Leaf Biomass", xlab="Theoretical Quantiles of the Leaf
Biomass", ylab="Sample Quantiles of the Leaf Biomass")
Add the theoretical line that the data should qqline(w1$vals)
fall on if they were normally distributed