You are on page 1of 2

1) NOs are treated as double 2) If a number wanted to be defined specifically as an integer then we need to add suffix L; For eg:

2L will denote integer 2 3) Inf is infinity 4) NAN is undefined value (0/0) 5) General functions are called attributes () which allows to set or modify attributes for R objects 6) We can use print(x) or just type x to get the values printed 7) <- assignment operator 8) # comments 9) : denotes sequence for eg: x <- 1:20 implies x is a sequence of 1 to 20 10) C fn is like concatenation used to create vector objects for eg: x<- c(0.5,0.6), <- c(T,F), <- c(a, b, c) etc 11) Vector function should have same objects as class for eg x<- vector (numeric, length =10) will print 10 0z; 12) We can convert using as function for eg: x <- 0:6; >class(x) will print integer; > as.numeric(x); as.logical(x) will convert to T, F; as.character(x) will print this as character; as.complex(x) will print as 0+0i, 1+0i etc 13) Matrix can be created by 3 ways a) m<- matrix (1:6, nrow =2, ncol =3) or <- matrix (1:6, 2,3) or m<- 1:6, dim(m) <- c(2,5) where dim indicates dimension function 14) Cbind, rbind binds rows & col of diff matrix 15) List is similar to vector but can have objects of different class for eg: > x<- list (1, a, T, 1+4i) now >x will print [[1]] [1] 1, [[2]] [1] a, [[3]] [1] T etc 16) We can also name a list objects like x <- list (a=2, b=3, c=4) will result in printing x as $a 2, $b 3, $c 4 17) Factors: Special type of lists (used to indicate with names and used for sorting counting, grouping etc) 18) Ctrl+l for clearing screen 19) For eg: x<- factor(c(Y, N, Y, N, Y)) then X will print as Y, N, Y, N, Y and will Indicate Levels: N, Y 20) Table(x) will give frequency N: 2, Y: 3 21) Unclass(x) will indicate how R takes for eg: 2,1,2,1,2 where 1 rep N, 2 rep Y 22) Levels are labeled on alphabetical order so if we want exclusive order then we must declare say x<- factor(c(Y, N, Y, N, Y), levels = c(Y, N)) 23) Missing Values: is.na() & is.NaN() returns logic however is.Na can be int NA, Char NA etc 24) Data Frames: Unlike a matrix can have any class, gen to store tabular data, have special attributes row.names, Can be created by read.table() or read.csv(); Can be converted to matrix by data.matrix() 25) Names: R Objects can have names for reference, as said x<- list( a=1, b=2, c=3) or x<- 1:3, name(x) <-c(a, b, c); 26) Naming row & colomn of matrix: m<- matrix (1:4, 2,2); dimnames(m) <- list(c(a, b), c(c, d)) will return m as a, b row names & c, d as colomn names with 1,2,3,4 as elements

27) Subsets: [, [[, $; if we use [ we return same object as class, for eg if its vector it returns a vector, if its a list it returns a list, whereas [[ extracts list or data frame, $ to extract element using name 28) Subsetting a matrix; x<- matrix (1:6, 2, 3); > x[1,] will give 1 3 5( not a matrix format but as vector as object so in order to get in matrix format as 1 row 3 colomn, > x[1, , drop = F] 29) Subsetting a List: x<- list (a = 1:4, b = 0.6) if we use x[1] it will return a list 1,2,3,4 but if we need the exact object 1,2,3,4 then we should use x[[1]] or x$a or x**a++ 30) Creating multiple events: x <- list( foo = 1:4, bar =0.6, baz =hello), x[c(1,3)] will result in $foo 1,2,3,4 & $baz Hello; Now when we assume variable to an object in the list then only use [[ as $ will take the variable name literally for eg: name <- foo; x[[name]] will return 1 2 3 4 whereas x$name will return NULL & x$foo will return 1,2,3,4 31) Partial Matching: x<- list(aardvark =1:5, abby = 2:7, andy = 3:10) then if we search x$aa will return 1,2,3,4,5 & x$an will return 3 to 10 whereas x$a & x[[aa]]will return NULL , we can use x**aa, exact = F++ will return 1,2,3,4,5 32) To remove missing values x<- c(1,2,NA, 4,NA,5); > bad <- is.na(x); x[bad!] will return 1 2 4 5 33) For multiple entries missing values use complete.cases(x) say for eg: x is same as above & y<c(a, b, NA, d, NA,f); then > good = complete.cases(x,y); x*good+ will give 1 2 4 5 & Y[good} will give a b d f 34) For true matrix multiplication we should use a %*% b where a & b are matrix, if we do not use % then it will do element multiplication

You might also like