# R as Calculator
5+4
log(10)
log(10,10)
5*4
#Using c() to combine data vectors
x = c(74, 122, 235, 111, 292)
y = c(111, 211, 133, 156, 79)
c(x,y)
[Link]()
?[Link]
apropos("dataframe")
help("vector")
#[Link]("tidyverse")
#library(tidyverse)
?vector
age <- c(1,3,5,2,11,9,3,9,12,3)
weight <- c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
mean(weight)
sd(weight)
cor(age,weight)
plot(age,weight)
q()
a <-c("k", "j", "h", "a", "c", "m")
a[3]
a[c(1,3,5)]
a[2:6]
# Define a Numeric Vector
x <- c(10, 11, 50, 80, 10.5, 29, 23, 8)
# Define a String Vector
y <- c("a", "and", "b", "are", "Strings")
# Count the number of elements (length) of the vector
length(x)
# Sum of the elements of the vector
sum(x)
# Summary of the Distribution of Variable x
summary(x)
# Sort the variables
sort(x)
sort(x, decreasing = TRUE)
#Vectorised Operations
a <- c(10, 9, 11, 90, 78, 89, 34)
b <- c(11, 92, 34, 67, 6, 99, 1)
# Sum of two vectors is also a vector
a+b
# Product of two vectors
a*b
# Square of a vector
a^2
# Division of the vectors
a/b
# Length of the larger vector is a multiple of the smaller
vec1 <- c(1,2,3,4)
vec2 <- c(1,2)
vec1+vec2
vec1/vec2
# Length of the larger vector is not a multiple of the smaller
x <- c(1,2,3)
y <- c(1,2)
x+y
x/y
# Naming Elements of a Vector
x <- c(10, 9, 11, 14, 15, 18, 19, 20)
a <- c("A", "B", "C", "D", "E", "F", "G", "H")
names(x) <- a
names(x)
#Slicing Elements from a Vector
# Extract the fourth element in x
x[4]
# Extract the elements from 4th to 6th position
x[4:6]
# Remove the third element from the vector
x[-3]
#Matrices
mymatrix <- matrix(vector, nrow = number_of_rows, ncol = number_of_columns,
byrow = logical_value, dimnames = list(
char_vector_rownames, char_vector_colnames))
#Creates a 5x4 matrix
y <- matrix(1:20, nrow = 5, ncol = 4)
cells <- c(1,26,24,68)
rnames <-c("R1", "R2" )
cnames <-c("C1","C2")
#Creating 2x2matrix filled by rows
mymatrix <-matrix(cells, nrow = 2, ncol = 2, byrow = TRUE,dimnames = list(rnames,cnames))
mymatrix
#Creating 2x2matrix filled by columns
mymatrix <-matrix(cells, nrow = 2, byrow = FALSE, dimnames = list(rnames, cnames))
mymatrix
x <- matrix(1:10, nrow = 2)
x[2,]
x[,2]
x[1,4]
x[1, c(4,5)]
# Construct Matrix by giving number of rows and columns
## Note: If not specified explicitly, R fills the matrix column-wise
m1 <- matrix(c(10, 11, 12, 13, 14, 15, 16, 17, 18), nrow = 3, ncol = 3)
m1
# Construct a Matrix filling it row-wise
m2 <- matrix(c(10, 11, 12, 13, 14, 15, 16, 17, 18), nrow = 3, ncol = 3, byrow = TRUE)
m2
# Creating a String Matrix
[Link] <- rep(c("a", "b","ZZ"),each=3)
[Link]
k <- matrix([Link], nrow = 3, ncol = 3)
# Basketball Data
Seasons <- c("2012", "2013", "2014")
Players <- c("KobeBryant","JoeJohnson","LeBronJames")
#Games
KobeBryant_G <- c(78,6,35)
JoeJohnson_G <- c(72,79,80)
LeBronJames_G <- c(76,77,69)
# Minutes Played
KobeBryant_MP <- c(3013,177,1207)
JoeJohnson_MP <- c(2642,2575,2791)
LeBronJames_MP <- c(2877,2902,2493)
# Create Matrix for Games and Minutes Played
Games <- rbind(KobeBryant_G, JoeJohnson_G, LeBronJames_G)
Games
Minutes_Played <- rbind(KobeBryant_MP, JoeJohnson_MP, LeBronJames_MP)
Minutes_Played
# Name the Rows and Columns of the Matrices
colnames(Games) <- Seasons
rownames(Games) <- Players
Games
colnames(Minutes_Played) <- Seasons
rownames(Minutes_Played) <- Players
Minutes_Played
# Calculate Number of minutes played in Each Game
round(Minutes_Played/Games)
# Extract the values of Games played only for the year 2014
Games[,3]
# How many games did Kobe Bryant play in the year 2013?
Games["KobeBryant", "2013"]
# The Other Method
Games[1,2]
# Generate sequence of numbers with the : operator
c(1:10, 20:30)
-5:5
5:-5
# Generate sequence of numbers with the seq() function
seq(10, 100, 10)
seq(10, 100, 3)
# Random Numbers
## Five Random numbers between 0 and 1
runif(5)
## A specified number of random numbers between two specified intervals
runif(5, 10, 20)
## Draw random numbers from a normal distribution
rnorm(10)
# Generate a Sequence of numbers
x <- 1:100
## Draw a particular number of samples
sample(x, 10)
## Draw samples with replacement
sample(x, 20, replace = TRUE)