Hellen Gakuruh
2017-03-07
By the end of this session we will have knowledge on:
R recognises seven data types, these are:
cont…
is.integer(c(-3L, 0L, 2L, 5L, 5L))
is.double(c(-3, 0, 2, 5, 6))
letters
c("a", "b", "c")
, letters
, c('cats', 'and' , 'dogs')
is.character
e.g. is.character(letters)
n
cat <- factor(c(rep("Y", 28), rep("N", 10)))
is.factor(cat)
[1] TRUE
levels(cat)
[1] "N" "Y"
n
# Example, complex vector
3+2i
[1] 3+2i
# Confirm it's complex
is.complex(3+2i)
[1] TRUE
int <- c(-3L, -2L, -1L, 0L, 1L, 2L, 3L)
is.integer(int)
[1] TRUE
is.numeric(int)
[1] TRUE
doub <- c(-3, -2, -1, 0, 1, 2, 3)
is.double(doub)
[1] TRUE
is.numeric(doub)
[1] TRUE
"type"
is used to establish a vector's type, function "length"
is used to determine length and function "attributes"
is used to get additional information about a vectoris.atomic()
or is.list()
. Note there is a is.vector()
but this checks if vector is namedcat <- c(rep("Y", 28), rep("N", 10))
typeof(cat)
[1] "character"
dim(cat)
NULL
is.matrix(cat)
[1] FALSE
dim(cat) <- c(19, 2)
typeof(cat)
[1] "character"
dim(cat)
[1] 19 2
is.matrix(cat)
[1] TRUE
"dim()"
to convert a one dim to a multi-dimension atomic vector, matrices can be created with "matrix()"
, or by coercing another data object with "as.matrix()"
typeof(airmiles)
[1] "double"
airmiles2 <- matrix(airmiles, nrow = 8, ncol = 3)
is.matrix(airmiles2)
[1] TRUE
airmiles3 <- as.matrix(airmiles, nrow = 8, ncol = 3)
is.matrix(airmiles3)
[1] TRUE
rm(airmiles2, airmiles3)
"EuStockMarkets"
typeof(AirPassengers)
[1] "double"
attr(AirPassengers, "class")
[1] "ts"
typeof(EuStockMarkets)
[1] "double"
attr(EuStockMarkets, "class")
[1] "mts" "ts" "matrix"
"dim()"
e.g. dim(a) <- c(6, 2, 2)
, or array()
or as.array()
"data frames"
and "lists"
n
# Example
head(faithful)
eruptions waiting
1 3.600 79
2 1.800 54
3 3.333 74
4 2.283 62
5 4.533 85
6 2.883 55
# See current objects
ls()
[1] "cat" "doub" "int"
# Store in an external .RData file
save.image()
# Remove all object from workspace/global environment
rm(list = ls())
ls()
character(0)
# Read in .RData
load(".RData")
# Check we have them back
ls()
[1] "cat" "doub" "int"
wrapper functions
(functions which provide a convinience interface to another function like give pre-defined/default values, this make function calls more efficient)read.csv()
, read.csv2()
, read.delim
, read.delim2
read.lines
function
Live demo (reading in CSV file)R Data Import/Export
specifically chapter 9.SELECT
statement.Live demo (reading in RDMS and web data)
"foreign"
other packages include, "readstata3"
and haven.Live demo (reading SPSS and Stata data files)