Basic Functions:

In R, # can be used for putting comment in a script.  (R ignores everything after # in a command line)

As a first step, you may set the current working directory.

#Examples:

setwd("d:/test")

setwd("/home/naimi/test")

 

getwd()# returns the current directory

 

Following functions show how R can be used as a calculator and how different operators including addition (+), subtraction (-), multiplication (*), division (/), exponentiation (^ or **), modulus (%%), integer division (%/%) can be used:

# R as calculator:

2*2

3*(1.4+12.2) /1.5

5^2

10 %% 3

10 %/% 3

pi

 

sqrt(25)

abs(-5)

log(2)# natural logarithm

log10(2)# common logarithm

exp(3)# e ^ 3

round(3.431)

round (3. 631 )

round (3. 431 , 1 )

sin(0. 5 )

cos(0. 5 )

sin( 0. 5 )/ cos(0.5)

tan(0. 5 )

Data types:

R supports different types of data objects (variables). Basic data types are:

vector, matrix, data.frame, factor, and list:

‘vector’ is the most basic data object. vactor stores a set of elements with the same type together in a vector (for example a set of numbers like 1,2,3,4). Assigning a single number to an object (e.g. x <- 12), will give you a vector containing single element. All objects have a mode and length. The mode determines the kind of data type (i.e. numeric, character, logical, complex). Length of an object is the number of elements in it, and can be obtained using length() function.

Data objects may contain a special data value, NA, means missing data.

#Examples:

x1 <- 10

x1

x2 <- "Babak"

x2

mode(x2)

length(x2)# returns number of items in vector v1

x3 <- TRUE

x3

mode(x3)

 

v1 <- c(11,23,12,10)

v1

mode(v1)

length(v1)# returns number of items in vector v1

 

v2 <- c(1:10)

v2

length(v2)

v3 <- seq(1,10,by=2)

v3

v4 <- c("Babak","John")

v4

mode(v4)

v5 <- c(T,T,F,T)

v5

v1 * 2

To access a particular element in a vector, you can put its index between square brackets in front of the name of objects (for example x[3] returns the third element in the vector of x; and x[1:3] returns the first three elements in vector x).

#Examples:

x <- c(5,100,115,200)

x

x[1]# First item in vector x

x[4]# 4th item in vector x

 

length(x)# number of items in x

x[length(x)]# last item in x, you see here how length() is used to get last item in x

 

x[1:3]# First three items in x

x[c(1,4)]# 1st and 4th items in x

 

# --- subsetting a vector

x[-1]# returns all items in x except first item!

x[-c(1,4)] # returns all items in x except 1st and 4th items!

x <- x[-1]# permanently remove first item from x!

To handle categorical (nominal) data, R supports a specific type of data, called ‘factor’. Suppose there is a vector contains character type values (e.g. "Forest", "Agriculture"…; or "Female", "Male"), then it can be presented as factor types because it offers an easier way to handle this type of data. Factors have levels which is the possible values in the factor.

#Examples:

f <- c("Forest","Agriculture","Agriculture","Forest","Agriculture")

f

mode(f)

 

f <- factor(f)

f  # f is a factor with two levels of Agriculture and Forest

table(f)

Data can be stored in objects with more than one dimenstion (like matrix and array).

m1 <- matrix(nrow=3,ncol=4)# a matrix with 3 rows and 4 columns

m1# returns values of the matrix m1

 

m1[] <- 1# assigns 1 to all elements of m1

m1

m1[1,1] <- 100# assigns 100 to element in row 1 and column 1

m1

 

class(m1)

dim(m1)

nrow(m1)

ncol(m1)

 

m1[] <- 1:12

m1

 

m1[2,3]# returns the value from the 2nd row and 3rd column in m1

m1[2,]# returns all values from 2nd row in m1

m1[,4]# returns all values from 4th column

m1[c(1,3),]# returns all values from 1st and 3rd rows in all columns of m1

 

# array is and extension of matrix with more than two dimensions

a <- array(1:18,c(3,3,2))

a[3,3,2]

m1 <- matrix(nrow=3,ncol=4)# a matrix with 3 rows and 4 columns

m1# returns values of the matrix m1

 

m1[] <- 1# assigns 1 to all elements of m1

m1

m1[1,1] <- 100# assigns 100 to element in row 1 and column 1

m1

 

class(m1)

dim(m1)

nrow(m1)

ncol(m1)

 

m1[] <- 1:12

m1

 

m2 <- matrix(c(1,1,2,2,3,3,4,4),nr=2,nc=4)

m2

m3 <- matrix(c(1,1,2,2,3,3,4,4),nr=2,nc=4,byrow=T)

m3

# what is the difference in creating of matrices m2 and m3 ?!

 

v1 <- c(0.1,0.5,1,2,3.5)

v2 <- seq(10,50,by=10)

v1 # v1 is a numeric vector

v2 # v2 is a numeric vector

# for combining the vectors v1 and v2 as two columns in a matrix, you can use cbind():

m4 <- cbind(v1,v2)

m4

class(m4)

 

rbind(m2,m3)# adding m3 rows to m2

 

m2 * m3 # component-wise multiplication -- Note that it is not a the usual matrix multiplication

 

t(m2)# transpose of matrix m2

 

m2 %*% t(m3)# matrix multiplication

 

A table form data can be stored as data.frame in R. A difference between matrix and data.frame is that in data frame, you can have columns with different types (e.g. character, numeric) but in matrix all should be the same.

c1 <- c("Bab","John","Marry")

c2 <- c("M","M","F")

c3 <- c(37,29,44)

 

df <- data.frame(c1,c2,c3)

df

class(df)

str(df)# structure of the object df

colnames(df)# names of columns

 

colnames(df) <- c("Name","Sex","Age") # changing the column names of df

df

df[,1] # first column of df

df[2,] # 2nd row of df

df$Age # the values of the column named Age in df