You are on page 1of 6

The Basics of R Programming Language Data Simulation

What is R?
#numeric variable
 R is a programming language; interpreted mygrade <- 95
mygrade
language
 Software environment for statistical analysis, #logical variable
graphics representation and reporting. canvote <- TRUE
canvote
 Created by Ross Ihaka and Robert Gentleman
at the University of Auckland, New Zealand #remove variable in the memory
 Currently developed by the R Development rm(x)
Core Team.
#display data type
class(mygrade)
class(canvote)
Variables
#integer variable
 Name given to a memory location, which is
score <- 46L
used to store values in a computer program. score
 Variables in R programming can be used to class(score)
store numbers (real and complex), words,
#complex variable
matrices, and even tables. mydata <- 3+5i
 Example: x <- 5 class(mydata)
o variable x has 5 as its value
#character variable
o Variable names are case sensitive (X ! mychar <- "Hello World"
= x) mychar
myaverage <- "95"
DATA TYPES class(myaverage)

Logical – TRUE, FALSE #raw variable


Numeric – 5,5.6,1005 myvar <- charToRaw("Hello World")
Integer – 2,3,4,5 print (myvar)
Complex – 2+3i # =, <- assignment operators
Character – “Hello” w = 17
Raw - Bytes print(w)

#multiple assignment
r <- f <- 5
print(r)
print(f)

# create a sequence
myseq <- 1:250
myseq

#built-in functions in R
pi
sqrt(16)
Data Structures in R Operators in R
What is Data Structure?
Arithmetic Relationa
 Collection of data (collection of logical,
+ = Addition l
numeric, integer, complex, character or raw
- = Subtraction <
data)
* = Multiplication >
 Deal with how the data is stored together
/ = Division ==
 R Data Structures include the Vector, Lists,
%% = Remainder <=
Matrix, Arrays, Data Frames and Factors
^ = Exponentiation >=
Vectors
!=
 collection of similar types of objects
 each element must belong to the same data
type
Example:
> vehicles = c("car","bike","bus")

Creates a vector of character named vehicles

c means coerce -> values are converted to the Accessing Vector Elements
simplest type required to represent all information
 Use [] brackets to access elements, This is
known as indexing. Indexing starts with
The ordering is logical<integer<numeric<character
position 1.
Vector with Consecutive Number  Negative values in the index are used to drop
elements
The : operator creates a vector of consecutive  Boolean values, TRUE or FALSE can be used
numbers for indexing.

The seq() function generates a sequence of number

Syntax – seq(from, to, by, length.out)

from – beginning of the sequence Matrix


to – end of the sequence
by – increment by (default is 1)  Collection of elements of the same data type
length.out – length of the sequence (numeric, character, or logical) arranged into
a fixed number of rows and columns.
 Since you are only working with rows and
columns, a matrix is called two-dimensional.
 You can construct a matrix in R with the
matrix() function.
Matrix

Syntax: matrix (data,nrow,ncol,byrow,dimnames)

Data: Elements of a matrix


Nrow: Number of rows
Ncol: Number of columns
Byrow: If TRUE then elements are arranged in a row
Dimnames: Names assigned to rows and columns
myvector = c(2,4,6,8,10)
fe = myvector[c(4)]
print(fe)
fee = myvector[c(1,4)]
print(fee)

lasttwo = myvector[c(4,5)]
print(lasttwo)

fbnum = c(22, 25, 78, 18, 7, 69)


fbnum[c(-5)]
Accessing Matrix Elements print(fbnum)
m[ ]
fbnum = c(22, 25, 78, 18, 7, 69)
fbnum[c(FALSE, TRUE, FALSE, FALSE,
TRUE)]

m = matrix(1:9, 3, 3,)
m
m = matrix(1:9, 3, 3, byrow = TRUE)
print(m)

r = c("r1", "r2", "r3", "r4", "r5")


c = c("c1", "c2")
m = matrix(1:10, 5, 2, byrow =
TRUE,dimnames=list(r,c))
Data Simulation m

vehicles = c("car", "bike", "bus") print(m[3,2])


class(vehicles) print(m["r5","c2"])
vehicles = c("car", 5, "bus")
print(vehicles) print(m)
fav = c(7, 1.2, TRUE) m["r3", "c2"] = 66
print(fav) print(m)
vehicles = c("car", "bike", "bus")
a = seq(from=5, to=55, by=10) vehicles[2] = "tricycle"
print(a) print(vehicles)
b = seq(3, 15, 4) vehicles
print(b)

c = seq(7, 25, length.out = 5)


print(c)

r = 7%%2
print(r)

ex = 5^2
print(ex)

v = c(1,2,3)
t = c(4,5,6)
class(v)
class(t)
vt= v+t
print(vt)

answer <- 22 == 22
print(answer)
answer <- 22 == "Twenty two"
print(answer)
answer <- 22 != "Twenty two"
print(answer)
Data Structures Part 2
Accessing Data Frames
Data Frame
Data
• A table or two-dimensional array-like structure in
which each column contains values of one variable
and each row contains one set of values from each
column

 Selecting 1 column

 Selecting 1 row

Creating Data Frame

Syntax: data.frame(#data)  Selecting 2 rows

Modifying Data Frame Values

Data • Modify salary values

Accessing Data Frames

Data
• Getting the summary

Arrays

• Using the $ operator  Multidimensional collection of elements


having the same data type
 Syntax: array(data, dim, dimnames)
 Using the $
operator with index
 A list can also contain a matrix or a function
as its elements.
 List is created using list() function

Factors
 Data objects which are used to categorize the
data and store it as levels
Arrays with Dimension Names  Factors are created using the factor ()
function by taking a vector as input

Data Frame
sname = c("Sam", "Dominic", "Diony")
block = c("C", "A", "B")
code = c(3000, 1000, 2000)
class(sname)
Accessing Arrays class(block)
-Element of row 2, column 1, matrix 1 class(code)

data.frame(sname, block, code)


df = data.frame(sname, block, code)
- Element of row 2, columns 2, matrix 1, 2 print(df)
class(df)

- Element of row 2, columns print(df$block)


print(df$block[3])

df[1]
df[3,]
df[c(1,2,3),]
1,2,3 matrix 1, 2
- Use dimension name df$code <- df$code + 1
List df
 R objects which contain elements of different
types like − numbers, strings, vectors and print(summary(df))
str(df)

Array

myarray = array(data = c("atc",


"taag", "att"), c(3,3))
print(myarray)

rname = c("r1", "r2", "r3")


cname = c("c1", "c2", "c3")
mname = c("m1", "m2")
myarray = array(data = c("atc",
"taag", "att"), c(3,3,2), dimnames =
list(rname, cname, mname))
print(myarray)
myarray[2,2,2]
another list inside it. myarray[2,,]
myarray["r3",,]

List
Factors
direction = c("N", "S", "E", "W", "N",
"E", "W", "N")
f_direction = factor(direction)
str(f_direction)
From LMS

pname = c("John Cruz", "Jack Bauer",


"Joe Klay")
bpay = c(12000.5, 9500, 10000)
dept = c("accounting", "maintenance",
"administrative")
df1 = data.frame(pname, bpay, dept)
summary(df1)
class(pname)
class(df1)
df1
print(df1$dept)
print(df1$dept[3])
print(df1[3])
print(df1[3,])
print(df1[c(1,3),])
df1
df1$dept = "Technical"
df1

ar = array(data = c(1:12), c(3,4,3))


print(ar)
rn = c("rec1", "rec2", "rec3")
cn = c("score1", "score2", "score3",
"score4")
mn = c("first matrix", "second matrix")
ar2 = array(data = c(1:12), c(3,4,2),
dimnames = list(rn, cn, mn))
print(ar2)
print(ar2[2,2,1])
print(ar2[2,2,])
print(ar2[2,,])
ar2
print(ar2["rec3","score4","second
matrix"])
mylist = list("Bag", TRUE, c("A", "B"))
print(mylist)
mylist[2] = FALSE
print(mylist)

status = c("ON", "OFF", "OFF", "ON",


"ON", "ON", "ON")
f_status = factor(status)
class(f_status)
str(f_status)

You might also like