FW 680A4 (Wildlife Ecology Modeling)

We will regularly use the R programming language to manipulate data and objects, create figures, and use functions.


I expect students to know some fundamentals of base R.


I am not expecting advanced knowledge of highly specialized and efficient programming.


More so, I don’t want learning base R to be an impediment of understanding what we are trying to accomplish.

Objects

Some things I expect students to know

  • what an object is

  • how to subset and manipulate different types of objects

    • vectors, lists, matrices, dataframes, etc.

E.g…..

# A vector of integers stored as an object...

vec = seq(1,10)

vec
 [1]  1  2  3  4  5  6  7  8  9 10
# A Matrix...

mat = matrix(vec,nrow=2,ncol=5)

mat[2,]
[1]  2  4  6  8 10
# A list

my.list = list(vec, mat)

my.list[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

Access

Accessing a dataframe

E.g…..

head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Access

Accessing a dataframe

E.g…..

mtcars$mpg
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
[31] 15.0 21.4
mtcars[,1]
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
[31] 15.0 21.4
mtcars['mpg']
                     mpg
Mazda RX4           21.0
Mazda RX4 Wag       21.0
Datsun 710          22.8
Hornet 4 Drive      21.4
Hornet Sportabout   18.7
Valiant             18.1
Duster 360          14.3
Merc 240D           24.4
Merc 230            22.8
Merc 280            19.2
Merc 280C           17.8
Merc 450SE          16.4
Merc 450SL          17.3
Merc 450SLC         15.2
Cadillac Fleetwood  10.4
Lincoln Continental 10.4
Chrysler Imperial   14.7
Fiat 128            32.4
Honda Civic         30.4
Toyota Corolla      33.9
Toyota Corona       21.5
Dodge Challenger    15.5
AMC Javelin         15.2
Camaro Z28          13.3
Pontiac Firebird    19.2
Fiat X1-9           27.3
Porsche 914-2       26.0
Lotus Europa        30.4
Ford Pantera L      15.8
Ferrari Dino        19.7
Maserati Bora       15.0
Volvo 142E          21.4

Indexing

Indexing a dataframe

E.g…..

# find the location of certain values

which(mtcars$mpg>20)
 [1]  1  2  3  4  8  9 18 19 20 21 26 27 28 32
# manipulating those elements

mtcars$mpg[which(mtcars$mpg>20)] = NA
mtcars$mpg
 [1]   NA   NA   NA   NA 18.7 18.1 14.3   NA   NA 19.2 17.8 16.4 17.3 15.2 10.4
[16] 10.4 14.7   NA   NA   NA   NA 15.5 15.2 13.3 19.2   NA   NA   NA 15.8 19.7
[31] 15.0   NA

function

What a function is

  • how to specify attributes of a function
  • how to wrap functions

E.g…..

# Specifying attributes of a function

mean(mtcars$mpg,
     na.rm=TRUE
     )
[1] 15.9

function

E.g…..

# Wrapping functions

sim = rnorm(10, 
            mean = mean(mtcars$mpg,na.rm=TRUE),
            sd = sd(mtcars$mpg,na.rm=TRUE)
)
 
sim
 [1] 17.72882 14.86549 19.22804 14.58197 14.99671 15.32980 21.51015 18.36904
 [9] 16.48433 17.26201

Last

Everyone codes differently.


You may use tidy over base R. That is fine.


If you are concerned about coding, talk to Brian (202A Wagar)


If you need resources for learning R, please go here and click on the tab Learning R.