"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

August 17, 2015

R Notes

Matrices
Tip #1 
Declaration - matrix (0,3,4)
3 rows, 4 columns and values 0
print[1,1]

Tip #2
Vector a <- 1:12 (To fill Matrix)
matrix(a, 3, 4)

Tip #3

Assigning, Fetching & Printing values
plank <- 1:8 - Create Vector
dim(plank) <- c(2,4) - Assign Dimension
print(plank) - Print values
plank[1,4] <- 0 - Assign value
plank[2,] - Fetch all 2nd row values

Tip #4 - Plotting Matrix

elevation <- matrix(2,5,10)
contour(elevation) - 2D representation
persp(elevation) - 3D representation
persp(elevation, expand = 0.2)

Statistics
Tip #1 - Mean computation
a <- c(1,2,3,4,5)
mean(a)
barplot(a)
median(a)

Tip #2
Horizontal line across plots
abline(h=mean(a))
abline(h=median(a))

Tip #3 
Standard Deviation
deviation <- sd(a)

Factors
Tip #1 
R has special collection called factors
chests <- c('gold', 'silver', 'gems', 'gold', 'gems')
types <- factor(chests)
print(types)

Data Frame
Similar to Database
Tip #1 - Loading data from files
read.csv("data.csv")
read.table("a.txt",sep="\t")

Tip #2 - Merge Data Frames
data1 <- read.csv("data.csv")
data2 <- read.table("a.txt",sep="\t")
merge(x = data1, y = data2)

Real World Examples
Data1 <- read.csv("a.csv")
Data2 <- read.table("b.txt", sep="  ", header=TRUE)
TargetData <- merge(x = Data1, y = Data2)
plot(TargetData$Data1, TargetData$Data2)

No comments: