Sei sulla pagina 1di 2

LESSON1:

Objects:

1. Assign value to object: x = 1, or x <- 1 (case sensitive, meaning x and X are different objects),
2. Object names in R may include numbers or periods, e.g. x.1 <- 20
3. Objects can store character values e.g. xx <- “hello”, R will treat any number as a character if it is stored with
quotation e.g. xx <- “1”, 1 will be treated as character not a number
4. To check what objects are stored in R workspace: LS()c
5. Remove object from workspace: LS()
6. Delete all objects: RM(LS())

Arithmetic Operations

7. E.g x+y will add the values in x and y, similarly all arithmatic operations can be done like:
a. Sqrt(x) to find square root
b. Y^2 to square y
c. Log(y) to find log of y
d. Exp(y) to find antilog or exponent of y
e. Log2(x) to find log base 2 of x
f. Abs(-14) to find absolute value of -14 which will give 14 as output

Things to remember:

8. If you don’t complete the command like typing “print(x” and press enter, then R will give a + sign, the command
will execute once you close the bracket i.e. type “)” and then press enter.
9. Arrow key up will get you the last command, and hitting the arrow key up again will get the previous to the last
command and so on. And arrow down key will get the next command.
10. To comment a code, type # before the code

LESSON 2:

Vector:

11. Create vector using concatenate command: x <- c(1,2,3,4), y<-c(“a”,”b”)


12. To see sequence of values: 2:7 will give output 2,3,4,5,6,7
13. To create a sequence of values: Seq(from=1, to=10, by=2) will give output 1,3,5,7,9
14. To create a vector of repeated numbers: x <- rep(1, times=5), x vector stores five 1s; y<-rep(“hello”, times=3), y
stores three hello’s
15. To create vector of repeated sequence of numbers: x <- rep(seq(from=1, to=10, by=0.5), times=2)
16. To create vector of repeated sequence of characters: y <- rep(c(“m”,”f”),times=5)
17. Add/subtract/multiply/divide something to every element of the vector: x +/-/*/÷ 10
18. If the number of elements of vectors are same then vectors can be added/subtracted/multiplied/divided
19. Extract element from a vector using third bracket: x[3]
20. Extract all elements except a particular one: x[-3], extracts all element except the 3rd one
21. Extract few elements: x[c(1,3)], extracts element 1 and 3 from x vector
22. Extract first few elements: x[1:3], extracts first three elements
23. Extract all elements except few: x[c(-1,-3)], extracts all elements except 1st and 3rd
24. Extract all elements less than a value: x[x<5], extracts all elements whose values are lesser than 5
25. Length of a vector: length(x)
Matrix:

26. To create matrix: x <- matrix(x(1,2,3,4,5,6,7,8,9), nrow=3, byrow=TRUE


a. 1,2,3…9 are the elements
b. Nrow decides the number of rows
c. Byrow=TRUE (case sensitive and should be in caps), will assign the elements row wise i.e. 1,2,3 in first
row; 4,5,6 in 2nd row and so on
d. Byrow=FALSE (case sensitive and should be in caps), will assign the elements column wise i.e. 1,2,3 in
first column; 4,5,6 in 2nd column and so on
27. To extract elements of matrix
a. x[row number, column number] e.g. x[1,2] extracts row 1, column 2 element
b. x[row number,] e.g. x[1,] extracts all elements of row 1
c. x[,column number] e.g. x[,1] extracts all elements of column 1
d. x[c(row number, column number), column number] e.g. x[c(1,3), 2] extracts elements of row 1 and 3
from column 2
e. x[c(row number, column number), ] e.g. x[c(1,3), 2] extracts elements of row 1 and 3 from all columns
f. x[,c(row number, column number)] e.g. x[,c(1,3)] extracts column 1 and 3 of all rows
g. x[row number, c(row number, column number)] e.g. x[1,c(1,3)] extracts column 1 and 3 of 1st row
h. Deleting a particular row or column: x[-c(1), ] or x[,-c(1)]

Operations on matrix

i. X*10, multiplies 10 to all elements of matrix x


j. Multiplication of two matrices: x%*%y
k. Inverse of matrix: z<-solve(x)
28. Dimension of matrix: dim(x)

LESSON 3:

Importing data from .csv and .txt files

29. read.csv("link of file",header=T,na.strings="?") (CAUTION: use back slash “/” for links)
a. header=t, setting header to true will make the first row of the table as header variables
b. "na.strings" is used to tell R to treat the characters present in the data set as missing values
30. Step 1: setwd("Link of folder“) (CAUTION: use back slash “/” for links)
Step 2: d<-read.csv(“Filename.csv”)
31. Step 1: Library(ISLR)
Step 2: Attach(Auto)
32. To Read.table(file=”/link”,header=true, sep=”\t”)
a. Sep=”\t” tells R in what format the data is..”\t” says the data is tab separated and “,” says data is comma
separated
33. Looking the dataset in spreadsheet: fix(filename)
34. Dimension of the dataset: dim(filename)
35. Omitting the missing values of dataset: Filename=na.omit(filename)
36. Name of the variables: names(filename)
37. Clearing history: rm(list=ls())
38.

Potrebbero piacerti anche