FANDOM


Vectors and MatricesEdit

Each only allows one type of data type. You can specify for matrices:

  1. Rows
  2. Columns
 MatrixA <- matrix(1:10, nrow=2, ncol=1 

VectorisationEdit

When compared to for loops, vectors are much faster

 x <- rnorm(10000000000) 
 sumx <- sum(x)

When compared to:

 sumx <- 0  
 for (i in 10000000000) {sumx <- sumx + i} 

R will AUTOFILL by COLUMN using old values if the matrix isn't well defined.

Functions Edit

 length() 
 sort(), decreasing = FALSE 

To reverse the order of each element:

 rev() 

Letters is just a vector with 26 letters.

 letters[1:9] 

Order shows the indicies of the NAMES of the vector

 order() 

Rank shows the indicies of the ELEMENTS of the vector. NOTE: if having same value of ELEMENT, average is given

 rank() 

Merging Data FramesEdit

 cbind() 
 rbind() 

Merge by column name (if match, then merge)

 merge(X,Y,by=c("GENDER","Weight")) 

Merge by column name + place NA for no match

 merge(X,Y,by=c("GENDER","Weight"),all=TRUE) 

Merging rows can be done smartly: Eg: If some rows repeat, but some haven't:

 install.packages("gtools") 
 library(gtools) 
 smartbind(A, B) 

Apply Edit

For all COLUMNS: MARGIN = 2 For all ROWS: MARGIN = 1

Functions include sum mean ColSums() RowMeans()

 apply(X, MARGIN=2, FUN=sum) 

Relations Edit

 c(1,2)!=c(1,1) 

Each element wthe result is:

 FALSE TRUE 

Other operations:

 A<-c(4,5,2,7) 
 B<-c(2,1,7,3)
 C<-c(2,3,7)
 is.element(C,A)
          [1] TRUE FALSE TRUE
 all(A%in%B)
          [1] FALSE
 intersect(A,B)
          [1] 2 7
 union(A,B)
          [1] 4 5 2 7 1 3
 setdiff(A,B) 
          [1] 4 5 

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.