Professional Documents
Culture Documents
Lab Session 1
Running R interactively
Now load RStudio and see how many panes are in the window. Can you find the
“Console” pane? Try type in the following function call. What do you get?
1+
2
Just like Python, R is an interpreted language. That means that you can easily try out an
unknown function interactively in the RStudio console pane.
Yes, it is helpful to think in terms functions. What does the code below do?
-+-(1, 2)
If you know other programming languages, you may start using R for basic
calculations.
3 +
4
3 /
4
How about v?
v <- 2*x +
1
length(v)
This is how you calculate sample variance (We will cover it in the
lecture). Vectors can contain logical elements.
z <- x >
13
!z
Find out what x > 13
& x < 13 is. And
what is x > 13 | x <
13?
Vectors can contain strings.
labs <- paste0(c("X","Y"),
1:10)
Vector index
R is case-sensitive
Run
mean(x)
And then
MEAN(x)
Does it work?
Factors
state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa", "qld", "vic",
"nsw", "vic", "qld", "qld", "sa", "tas", "sa", "nt", "wa", "vic",
"qld", "nsw", "nsw", "wa", "sa", "act", "nsw", "vic", "vic",
"act") statef <- factor(state)
Being more flexible than vectors, a list can contain objects of different types. It is similar
to hashmap in some other languages but it is ordered.
Lst <- list(name="Fred", wife="Mary", no.children=3,
child.ages=c(4,7,9))
Lst[[1]]
Treat it as a hashmap.
Lst$wife
For analysts, data frames are probably the most important construct in R. You can think of
a data frame as a flat data table. If you read data from a file, most likely the result is
a data frame.
iris$Sepal.Length
Once you load the data. There are different ways to look at the data. Try the
following commands.
View(iris)
head(iris)
A data frame is actually a list of named vectors (of equal length). But you may
conceptualise it as a matrix.
dim(iris)
1. Now try to find a dataset and import the dataset into R. Find out the
number of observations and the number of variables. How is each
variable represented in R?
2. Please discuss with your classmates the following question.
• R organises data by vectors. What is the advantage of a vector-
based data structure?
4. KDnuggets