R Basics: Daniel Stegmueller

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

R Basics

Daniel Stegmueller
Nufeld College, University of Oxford

R Basics, [1]

Basic Structure

Object oriented Different types of object that contain different types of data: Scalar: numbers, characters, logical values Vector: sets of scalars Matrix: two-dimensional set of scalars of same type Array: multi-dimensional set of scalars of same type List: Combinations of scalars, vectors, matrices... Data frame: Collections of vectors of (possibly) different types but with same length

R Basics, [2]

Getting help
If you are unsure how a command works, you can always get help using

help(command)
or

?command
If you do not know a command you can search using

apropos("word")
Furthermore there is a package, that lets you search the internet for help

library(RSiteSearch) RSiteSearch("word")

R Basics, [2]

Working with objects


Several of those objects can be kept in memory at the same time To distinguish them, object have names Typing the name of an object shows its contents:

> a [1] 5 > dat socexp_t 1 17.87 2 25.33 3 25.30 4 16.73

vturn gov_left 95.00 0.000 80.40 4.430 90.60 55.556 60.54 0.000

We can see all objects currently in our workspace by typing

> ls() [1] "a" "dat"

R Basics, [3]

Operators
Assigning values to objects is done with <-

> a > b > c > c [1]

<- 5 <- 6 <- a * b 30

Several values are combined using c():

> a > b > c > c [1]

<- c(1,2,3) <- 2 <- a*b 2 4 6

Works also for characters:

> cntry <- c("DE", "FR", "NL") > cntry [1] "DE" "FR" "NL"

R Basics, [4]

Select elements from a vector / matrix


Using the [] construct we can indicate positions

> a[2] [1] 2 > a[2:3] [1] 2 3


Select elements from a matrix

> m <- cbind(a,c) > m a c [1,] 1 2 [2,] 2 4 [3,] 3 6 > m[1,1] a 1 > m[3,2] c 6

R Basics, [5]

Select elements from a list


> list1 <- list(a,d) > list1 [[1]] [1] 1 2 3 [[2]] [1] "a" "b" "c" > list1[[1]] [1] 1 2 3 > list1[[2]] [1] "a" "b" "c"

R Basics, [6]

Select elements from data.frames


> dat socexp_t 41 17.87 89 25.33 137 25.30

vturn gov_left 95.00 0.000 80.40 4.430 90.60 55.556

> dat[1,2] [1] 95 > dat[1:3,2] [1] 95.0 80.4 90.6 > dat[1,"vturn"] [1] 95 > dat[ , "vturn"] [1] 95.00 80.40 90.60 [14] 86.50 73.30 84.80 > dat$vturn [1] 95.00 80.40 90.60 [14] 86.50 73.30 84.80

60.54 86.00 68.30 68.00 82.20 78.30 84.10 65.90 78.30 61.80 68.70 81.40 43.30 71.60 50.00 60.54 86.00 68.30 68.00 82.20 78.30 84.10 65.90 78.30 61.80 68.70 81.40 43.30 71.60 50.00

R Basics, [7]

Select elements based on conditions


Available conditions:

==

!=

<

>

<=

>=

Build more complex queries with AND and OR operators

&

> a[a >= 2] [1] 2 3 > new <- a[a >= 2] > a[a >= 2 & a != 3] [1] 2 c[a==2] [1] 4 m[a==3,] a c 3 6

R Basics, [8]

Loading data
Library for export/import

library(foreign)
SPSS data les

data <- read.spss("filename.spss")


Stata data les

data <- read.dta("filename.dta")


Comma delimited data

data <- read.csv("filename.csv")


Whitespace or tab delimited data

data <- read.table("filename.dat")

R Basics, [9]

Checking data
Quick look at structure and summary of data

> names(dat) [1] "socexp_t" "vturn" > str(dat) data.frame: $ socexp_t: num $ vturn : num $ gov_left: num > summary(dat) socexp_t Min. :13.6 1st Qu.:18.0 Median :20.4 Mean :20.8 3rd Qu.:24.2

"gov_left"

23 obs. of 3 variables: 17.9 25.3 25.3 16.7 25.8 ... 95 80.4 90.6 60.5 86 ... 0 4.43 55.56 0 76.19 ...

vturn Min. :43.3 1st Qu.:67.0 Median :78.3 Mean :74.1 3rd Qu.:83.5

gov_left Min. : 0.0 1st Qu.: 0.0 Median : 50.0 Mean : 46.2 3rd Qu.: 89.6

R Basics, [10]

Manipulating data
Create a new variable in data.frame dat

> dat$new <> dat socexp_t 1 17.87 2 25.33 3 25.30 4 16.73 5 25.75

1 vturn gov_left new 95.00 0.000 1 80.40 4.430 1 90.60 55.556 1 60.54 0.000 1 86.00 76.190 1

Recode a variable based on condition

> dat$new[dat$vturn > 90] <- 2 > dat socexp_t vturn gov_left new 1 17.87 95.00 0.000 2 2 25.33 80.40 4.430 1 3 25.30 90.60 55.556 2 4 16.73 60.54 0.000 1 5 25.75 86.00 76.190 1

R Basics, [11]

Functions
Many tasks in R are carried out using function calls They look like mathematical functions for one or several variables

log(x) plot(a, b)
Every function has a name followed by parentheses and a set of arguments The plot function above expects a variable for the x and y axis. By just giving a and b we let R assume that the rst arguments is for x and the 2nd one for y. This is called position matching. To use keyword matching of arguments wed write:

plot(x=a, y=b)
Further arguments to function can be given using position and keywords

plot(a, b, pch=19)

R Basics, [12]

Functions II

Even with no arguments you have to use the parentheses

ls()
Omitting parentheses shows you the code of the function To get a quick list of all arguments use args()

> args(plot.default) function (x, y = NULL, type = "p", xlim = NULL, ylim = NULL, log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ann = par("ann"), axes = TRUE, frame.plot = axes, panel.first = NULL, panel.last = NULL, asp = NA, ...)
Note that most arguments have default values

You might also like