Data Visualisation
L9+L10
Lab 1
R Basics
PRINTING CHARACTER
print("Hello World")
[1] "Hello World"
print(23.9+56.6)
[1] 80.5
mystring<-"Hello World!"
print(mystring)
[1] "Hello World!"
PRINTING BOOLEAN
v <- TRUE
print(class(v)) #class function tells category of
the input
[1] "logical"
PRINTING COMPLEX
a<- 3+2i
print(class(a))
[1] "complex"
PRINTING INTEGER
i <- 253L
print(class(i))
[1] "integer"
VECTOR CHARACTER
vectora<-c('red','orange','kyoto')
print(vectora)
[1] "red" "orange" "kyoto"
print(class(vectora))
[1] "character"
VECTOR NUMERIC
vectorb<-c(1,2,3,4.5) #Here it shows
numeric instead of
integer because we have
included a decimal as
well
print(class(vectorb))
[1] "numeric"
VECTOR LOGICAL
vectorc<-c(TRUE,FALSE,TRUE,TRUE)
print(class(vectorc))
[1] "logical"
VECTOR RAW
vectord<-c(charToRaw("My Name is Anthony
Gonsalves!"),charToRaw("Why This Kolaveri
D"))
print(class(vectord)) #charToRaw converts to
hex value
[1] "raw"
v<-charToRaw("My Name is Akhil")
print(v)
[1] 4d 79 20 4e 61 6d 65 20 69 73 20 41 6b 68
69 6c
#hex value for space is
20
LIST WITH DIFFERENT TYPES
listmix1<-list(c(243,23,1.3),4,cos)
print(listmix1) #Making a list with different
data types
[[1]]
[1] 243.0 23.0 1.3
[[2]]
[1] 4
[[3]]
function (x) .Primitive("cos")
LIST WITHIN LIST
listdoublemix<-list(listmix1,c(1,2,3,sin))
#Making a list having the
previous list as an
element. List() function
converts all elements
stringed into a list
print(listdoublemix)
[[1]]
[[1]][[1]]
[1] 243.0 23.0 1.3
[[1]][[2]]
[1] 4
[[1]][[3]]
function (x) .Primitive("cos")
[[2]]
[[2]][[1]]
[1] 1
[[2]][[2]]
[1] 2
[[2]][[3]]
[1] 3
[[2]][[4]]
function (x) .Primitive("sin")
MATRIX
mat = matrix(c('a','b','c','d'),nrow = 2, ncol
=2,byrow = TRUE)
#matrix() function converts the list
given in c() to a matrix with
the help of specifiers nrow and ncol
and the byrow variable.
mat
[,1] [,2]
[1,] "a" "b"
[2,] "c" "d"
print(mat)
[,1] [,2]
[1,] "a" "b"
[2,] "c" "d"
print(class(mat)
+)
[1] "matrix"
ARRAY
arr <- array(c('green','potato'),dim = c(3,3,2))
#using c() to create an array with dim
specifier that specifies the number
of dimensions and elements in
those dimensions. Then the list in c() is
scaled and mapped accros all points
in the dimensions.
print(arr)
,,1
[,1] [,2] [,3]
[1,] "green" "potato" "green"
[2,] "potato" "green" "potato"
[3,] "green" "potato" "green"
,,2
[,1] [,2] [,3]
[1,] "potato" "green" "potato"
[2,] "green" "potato" "green"
[3,] "potato" "green" "potato"
arr <- array(c('green',123),dim = c(3,3,2))
print(arr) #Trying array with different data
types
,,1
[,1] [,2] [,3]
[1,] "green" "123" "green"
[2,] "123" "green" "123"
[3,] "green" "123" "green"
,,2
[,1] [,2] [,3]
[1,] "123" "green" "123"
[2,] "green" "123" "green"
[3,] "123" "green" "123"
print(class(arr))
[1] "array"
DATA FRAMES
BMI<- data.frame(
+ gender = c("Male","Female","Male"),
+ height = c(152,146,133),
+ weight = c(233,343,112),
+ age = c(34,56,12))
#Organising data in a table
using frame
BMI
OUTPUT:
gender height weight age
1 Male 152 233 34
2 Female 146 343 56
3 Male 133 112 12
VARIABLES
var.1 = c(1,2,3,4)
.var2 = c("Akhil","Tripathi")
c(FALSE,0) -var3.
print(var.1)
[1] 1 2 3 4
cat("var.1 is ",var.1,"\n")
var.1 is 1 2 3 4
cat(".var2 is ",.var2,"\n")
.var2 is Akhil Tripathi
cat("var3. is ",var3.,"\n") #Trying diff naming
methods
var3. Is 0 0 #Checking data types
class(var.1)
[1] "numeric"
class(.var2)
[1] "character"
class(var3.)
[1] "numeric"
print(ls()) #Checking all available
varialbles in program
[1] "a" "apple" "arr" "BMI"
[5] "factor_apple" "i" "listdoublemix"
"listmix1"
[9] "mat" "mystring" "v"
"var.1"
[13] "var3." "vectora" "vectorb"
"vectorc"
[17] "vectord"
print(ls(pattern = "var"))
[1] "var.1" "var3."
print(ls(pattern = ".var"))
character(0)
print(ls(all.name = TRUE))
[1] "a" "apple" "arr" "BMI"
[5] "factor_apple" "i" "listdoublemix"
"listmix1"
[9] "mat" "mystring" "v"
"var.1"
[13] ".var2" "var3." "vectora"
"vectorb"
[17] "vectorc" "vectord"