This is a summary of the "R"-part of the course: Programming for economists given at Tilburg University. The documents contains a summary of the theory and codes needed to do the exercises in R.
Select rows and columns
matrix[1:3, 2] # selects rows 1, 2 and 3 and second column
matrix[,1] # selects all elements of the first column
Factors
Statistical data type used to store categorical variables
A categorical variable can belong to only a limited number of
categories, but it can correspond to an infinite number of values
factor(vector, order = TRUE, levels = c(“Low”, “Medium”, “High”))
# R assigns the factor levels in alphabetical order
summary()
factor2 <- factor_created_vector[2]
Dataframes
Dataframes: 2d object; can hold numeric, character or logical values.
Within a column all elements have the same data type, but different
columns can be of different data type
df[1,3] # selects first row and third column
df[4,] # selects entire fourth row
df[1:5, “column_name”] # select first 5 values of column_name
or: df$column_name
subset(my_df, subset = some_condition)
order() # gives the ranked position of each element
e.g. a <- c(100, 10, 1000)
order(a) [1] 2 1 3
a[order(a)] [1] 10 100 1000
Example: planets_df => order diameter low-high
position <- order(planets_df$diameter)
planets_df[position, ]
Lists
List: different items in the list differ in length, characteristic and
type. Gathers a variety of objects under one name in an ordered way
(matrices, vectors, dataframes, etc.)
filter() : subset observatiFons
e.g. dataset %>%
filter(year == 2007, country == “Germany”)
arrange() : sorts a table based on a variable
e.g. dataset %>%
arrange(column_name) # in descending order: arrange(desc(column))
mutate() : mutate changes or add variables
e.g. dataset %>%
(change) mutate(pop = pop / 1000000) %>%
(add) mutate(gdp = gdpPercap * pop)
summarize() : turns many rows into one
e.g. dataset %>%
filter(year == 2007) %>%
summarize(meanLifeExp = mean(lifeExp), totalPop = sum(pop))
other functions for summarizing: median, min, max
group_by() : before summarize() turns groups into one row each
e.g. dataset %>%
group_by(year, continent) %>%
summarize(meanLifeExp = mean(lifeExp), totalPop = sum(pop))
Visualizing with ggplot2
library(ggplot2)
ggplot(dataset, aes(x = , y =, color = , size = ))
+ geom_point() + scale_x_log10()+ facet_wrap(~ sort) +
expand_limits(y=0)
More types of plots:
geom_line()
geom_col() # bar plot
geom_histogram(binwidth = ) # you only have to specify x =
geom_boxplot()
ggtitle(“”)
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller lisaholling1. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $3.21. You're not tied to anything after your purchase.