R - Others
Topic
- Basic
# => Comment
max(varV) => Returns maximum value
min(varV) => Returns minimum value
log(10) => Computes common logarithms
log(n, base=n)
var = n + im => Complex value
Re(var)
Im(var)
Conj(var) => Complex conjugate
Mod(var) => Complex Modulus
Arg(var) => Complex Argument
solve(n,m) => Solves nx=m and returns value of x
mode(varV) => Type of storage
mean(varV) => Calculates Mean
median(varV) => Calculates Median
var(varV) => Calculates Variance
summary(varV) => Gives various information about the vector
cumsum(varV) => Calculates cumulative sum
- Data Types
var <- value => Value is assigned to Variable
value -> var
var <- NA => Missing Observations
is.na(var) => Returns true if missing data, If value is set to NA
var = value
- Vector
varV = c(1,2,3,4,5) => Combines the numbers 1,2,3,4 and 5 to a vector
varV[n] => Returns nth element, Index starts from 1
varV[c(n1,n2,n3)] => Returns n1, n2, n3 element
varV[-n] => Returns all but the nth element
varV[n1:n2] => Returns elements from n1 to n2 index
sort(varV) => Sorts the values of vector in ascending order by default
sort(varV, decreasing=TRUE) => Sorts the values of vector in descending order
length(varV) => Gets or sets the length of a vector (list) or other objects
varV[varV > n] => Returns elements greater than n
varV[(varV%%2)==0] => Returns only even elements
varV[varv %in% n1:n2] => Returns elements with values from n1 to n2
- Set Operations
union(varV1, varV2) => Returns union of both vectors
c(varV1, varV2) => Returns concatenation of both vectors
intersect(varV1, varV2) => Returns intersection of both vectors
setdiff(varV1, varV2) => Returns difference of both vectors
- Matrix
varM <- matrix(nrow=n, ncol=m, data=c(n1, n2, n3, n4)) => Create a matrix, Data entered column-wise
var[n,m] => Access the element in this position
var <- matrix(nrow=n, ncol=m, data=c(n1, n2, n3, n4), byrow=TRUE) => Create a matrix, Data entered row-wise
dim(var) => Dimension of matrix
nrow(var) => Number of rows
ncol(var) => Number of column
attributes(var) => Informs the dimension of matrix
var <- matrix(nrow=n, ncol=m, data=n) => Create a matrix, Data entered to all is n
var <- diag(1, nrow=n, ncol=n) => Create a identity matrix of size n
var2 <- -t (var1) => Transpose of a Matrix
var*n => Multiplication of a matrix with constant n
var3 <- var1 %*% var2 => Matrix Multiplication
var2 <- crossprod(var1) => Cross Product of matrix with a Function
var1 + var2 => Addition of matrix of same Dimension
var[n, ] => Access the nth row
var[ ,m] => Access the mth column
var[n1:n2, m1:m2] => Access m1 to m2 column of n1 to n2 row
solve(var) => Finds the inverse of a positive definite matrix
eigen(var) => Finds the eigen values and eigen vectors of a positive definite matrix
var <- matrix(1:6, n, m, byrow=T) => Create a matrix with values 1 to 6
var <- rbind(var, n1:n2) => Adds a row with n1 to n2 values
var <- cbind(var, m1:m2) => Adds a column with m1 to m2 values
var[n, ] <- n1:n2 => Change entire nth row from n1 to n2
var[ ,m] <- m1:m2 => Change entire mth column from m1 to m2
var <- var[-n, ] => Delete nth row
var <- var[ ,m] => Delete mth column
- Sequence
seq(from=n, to=m) => Generate a sequence from n to m
seq(n, m) => Generate a sequence from n to m
seq(from=n, to=m, by=o) => Generate a sequence from n to m with a gap of o
seq(n, m, by=o) => Generate a sequence from n to m with a gap of o
seq(from=n, length=m) => Generate a sequence from n up to length m
seq(n, m, o) => Generate a sequence from n to m with a gap of o
seq(as.Date("2021-11-11"), by=years) => Generating sequence of Dates
letters => Generates sequence of all letters from a to z
letters[n1:n2] => Generates letters from n1 to n2
letters[n2:n1] => Generates letters in reverse order from n2 to n1
LETTERS => Generates sequence of all uppercase letters from a to z
- Repeat
rep(n, times=m) => Repeats n m times
rep(n1:n2, times=m) => Repeats n1 to n2 m times
rep(n1:n2, each=o) => Repeats n1 m times, n2 m times and so on
rep(n1:n2, each=o, times=m) => Repeats n1 m times, n2 m times and so on, m times
rep(n1:n2, m1:m2) => Repeats n1 m1 times, n2 m2 times and so on
rep(n1:n2, seq(from=n, to=m, by=o)) => Repeats n1 n times, n2 n+o times and so on
rep(varM, m) => Repeats a matrix m times
rep(c("a","b","c"), m) => Repeats the given characters m times, It can also be string in place of character
- Conditional
ifelse(test, yes, no) => If test is TRUE then perform yes operation else no operation
for(i in n1:n2) {statement}
for(I in varV) {statement}
while(condition) {statement}
which(condition) => Selects which satisfies the condition
repeat {statement} => Does not check any condition
- Data Frame
library(MASS)
painters => Provides random data to work on
rownames(painters)
colnames(painters)
str(painters) => Returns structure of the Data
painters$var => Extract a variable from the data frame
subset(painters, painters$var=="value") => Get subset of a data frame
split(painters, painters$var) => Partitions the data set by values of a specific variable
painters$var = factor(painters$var, labels = c("value1", "value2")) => Sets values of variable in painters equal to given values based on its 1 & 0
table(painters$var) => Creates a one way table for this variable
table(painters$var1, painters$var2) => Creates a two way table for this variable
var <- data.frame(x, y, z) => Creates random data based on this
- Graph
plot(var, type="1")
plot(var, xlab="value1", ylab="value2") => Gives names to the x and y axis
plot(var, col="value")
plot(var, main="value") => Gives Title to the graph
pie(var) => Generates a Pie chart
barplot(var, beside=T) => Generates a Bar plot
legend=rownames(table5)
xlim=c(1,15),ylim=c(0,5)
col=c("blue","red")
boxplot(var1 ~ var2)
- Sort
- Repeat
- Operators
- Tables
Others
- Theory
- Install R
- Go to r-project.org > Click Download R > Click on link on O-cloud > Click on for Windows > Click Version
- Download R Studio
- Go to rstudio.com > Click Download R Studio > Click Desktop > Click on Free version > Select operating System
- Package
- Base => Installed with R but not loaded by Default
- Contributed => Need to be Downloaded, Installed & Loaded separately
- Shortcuts
- ctrl + Enter => Run Program
- ctrl + R => To execute highlighted line
- Console Commands
- license()
- demo()
- help()
- help.start()
- q() => Quit R