R Seminar 1
R Seminar 1
R Seminar 1
David Leiva
Session 1
Contents
Introduction to R
The R Environmnent
RStudio: An IDE for R
Getting started with R
R Help System
R packages
Exercises
The R Environmnent
What is R?
The R Environmnent
What is R?
The R Environmnent
What is R?
But R also has some drawbacks:
Its graphical interface is quite limited (GUI).
It mainly works under a command line interface (CLI).
Its learning can be cumbersome for some novice users.
What is this R code for? Histogram of x
x <- rnorm(100)
20
hist(x, col = "red")
15
Frequency
10
5
0
−3 −2 −1 0 1 2
The R Environmnent
Working with R
The R Environmnent
Working with R
The R Environmnent
Working with R
Available on http://www.rstudio.com/.
c() function
students <- c(84,74,67,90,78)
students
[1] 84 74 67 90 78
scan() function
income <- scan()
# COPY THESE DATA:
# 12 11 10 14 15 16 18 23 24 27 13 21 12
# THEN ENTER
Importing data
read.table() function
df1 <- read.table(file="visualMemory.txt",header=TRUE)
head(df1)
Importing data
read.csv() function
df2 <- read.csv(file="visualMemory.csv",
header=TRUE,sep=";")
head(df2)
Importing data
read_sav() function
#install.packages('haven')
library(haven)
df3 <- read_sav(file="visualMemory.sav")
head(df3,n=3)
# A tibble: 3 x 3
before after difference
<dbl> <dbl> <dbl>
1 1 11 10
2 2 11 9
3 3 11 8
Importing data
read_xlsx() function
#install.packages('readxl')
library(readxl)
df4 <- read_xlsx(path="visualMemory.xlsx",sheet=1)
colnames(df4)
Data in R: miscellanea
[] operator
Accessing to vector’s elements: vector[index]
students[1]
[1] 84
Accessing to data frame elements (and matrices):
dataframe[row,column]
df1[1,2]
[1] 11
Data in R: miscellanea
$ operator
Accessing to data frame variables:
dataframe$variable.
df1$before
[1] 1 2 3 3 2 1 1 2 3 2 1 1 2
[14] 3 2 3 1 1 1 2 2 1 2 1 2 3
[27] 2 2 1 1
Data in R: miscellanea
Working directory
A default directory is used in any R working session when
loading and saving data files and scripts.
Where is the working directory? getwd().
How can I change it? setwd(DIRNAME).
RStudio allows users to easily deal with:
Objects in global environment (i.e. Workspace).
Directories and files.
R Help System
Help Documentation
R Help System
Help Documentation
R Help System
Help Documentation
R Help System
Help Documentation
Vignettes
Another documentation is the so-called vignette.
Vignettes offer a more detailed description of packages.
vignette(package="xtable")
vignette("xtableGallery")
R Help System
Help Documentation
R Help System
Help Documentation
R Help System
Help Documentation
apropos() function
Suppose you are interested in doing a boxplot of frontal
lobe size FL variable included in crabs data set.
apropos() function can be used in order to find any
object that coincides with the argument given.
apropos("boxplot")
R Help System
Help Documentation
example() function
Generally, functions
documentation includes
examples.
It can be checked by means of
example() function.
example(boxplot)
R packages
R repositories
R packages
R repositories
R packages
R packages
install.packages() function
Contributed packages should be installed before being
used. Steps:
1 install.packages("psych"). NOTE: Package name
placed between quotation marks.
2 Choose a mirror.
Default repository is CRAN but users can change it.
installed.packages() function for checking what
packages are already installed.
update.packages() function looks for new versions of
installed packages, then installs it.
R packages
library() function
Every time you need to use a package in a new R working
session you have to load it.
It is done by: library(PACKAGE). NOTE: Quotation
marks are not required.
How to know what packages are already loaded in the
current session: loadedNamespaces() function.
Exercises
Exercise 1
Exercises
Exercise 2
Exercises
Exercise 3
Install the package stargazer, check its help doc and look for
information regarding the following:
a) What is the main purpose of this package?
b) What is the main function included in this package?
c) Check the Usage section for this function. How many
arguments does it have?
d) Create a summary table for dataset attitude, printing 3
decimal digits, to be exported to a LaTeX file and to an
HTML file.