2  Objects and Functions

2.1 Objects and Assignment

The joy of coding is that you can quickly to a lot of things at once. So, if I had a dataset with temperatures in Fahrenheit that I want to convert to Celsius, we can use R to do that all at once.

Let’s say our list of temperatures are: 72, 69, 57, 58, 71, 64, 65, 70, 59

We can use R like a calculator to convert each of these numbers

(72-32)*5/9
[1] 22.22222
(69-32)*5/9
[1] 20.55556
(57-32)*5/9
[1] 13.88889
(58-32)*5/9
[1] 14.44444
(71-32)*5/9
[1] 21.66667
(64-32)*5/9
[1] 17.77778
(65-32)*5/9
[1] 18.33333
(70-32)*5/9
[1] 21.11111
(59-32)*5/9
[1] 15

But that’s tedious. Instead, we can clump these numbers together into a vector and assign it to a variable.

Let’s call our vector “temps”

temps <- c(72, 69, 57, 58, 71, 64, 65, 70, 59)

temps
[1] 72 69 57 58 71 64 65 70 59

The <- is called the “assignment operator”. You can also use = to do the same thing. The c is short for “concatenate”, which means “stick all these things together”. We now have an object temps that is a vector of values. Type “temps” into your console.

We can then perform operations on the whole vector of values at once. For example

(temps-32)*5/9
[1] 22.22222 20.55556 13.88889 14.44444 21.66667 17.77778 18.33333 21.11111
[9] 15.00000

If we want to save that output, we need to assign it to a new variable

temps_C <- (temps-32)*5/9

Note that this doesn’t give you any output, it just assigns a value to temps_C. If we want to see what temps_C is, we need to print it out.

temps_C
[1] 22.22222 20.55556 13.88889 14.44444 21.66667 17.77778 18.33333 21.11111
[9] 15.00000

If you want to print it as you make the assignment, you can put parentheses around it.

(temps_C2 <- temps_C*2)
[1] 44.44444 41.11111 27.77778 28.88889 43.33333 35.55556 36.66667 42.22222
[9] 30.00000

You’ll also notice that in the Environment tab in your Rstudio window you should now have temps and temps_C. You can click on them to see them.

2.1.1 Exercise

Convert these numbers from miles per hour to meters per second. (use assignment)

21, 25, 100, 50, 36, 72, 15

Click below for the answer when you are done.

Code
#the variable "mph" will be assigned the value of a vector of all our numbers
mph <- c(21, 25, 100, 50, 36, 72, 15)

#to convert mph to mps, divide by 2.237
mps <- mph/2.237

#now print the output
mps

2.1.2 Type of objects

We can call temps and temps_C objects. Specifically, they are numeric vectors. Let’s go through some different types of objects.

  • Scalars - single value

  • Vectors - list if values (one dimensional), all the same data class

  • Matrices - values in rows and columns, all the same class (two dimensional)

  • Arrays - multiple matrices stacked up (three-dimensional)

  • Data Frames - Data of different types in rows and columns, where all columns are the same length and all rows are the same width. Data in the different columns can be of different classes.

  • Lists - data of different types and different lengths in rows and columns.

2.1.3 Data classes

  • numeric - Numbers (duh), these can be integers, real numbers, etc.

  • character - Any sequence of letters and numbers or special characters.

  • logical - TRUE/FALSE (always in all caps, or abbreviated T and F)

  • factor - Categorical variables that take a limited set of values. May be ordered (like water year types), or unordered (like families of fishes)

  • date/time classes (giant can of worms, we’ll talk about those later)

Using class and str to get info about objects. also View and head.

foo <- "Cat"
dog <- 24

class(foo)
[1] "character"
str(foo)
 chr "Cat"
class(dog)
[1] "numeric"
foo2 = c("Mouse", "Cat", "dog", "Squirrel")
str(foo2)
 chr [1:4] "Mouse" "Cat" "dog" "Squirrel"

2.1.4 Functions

Functions are little sequences of code that do something useful. There are lots of built-in functions, plus you can define your own functions when you get a little more practice.

The basic structure is:

function(arguments)

where “arguments” are the inputs to your function.

The parentheses are the “trigger” that tells R to run the function. Typing the name of the function without the parentheses prints the code in the console. And you can write your own when you get good.

#when we calculated the sum of 1 +1, that was a function

sum(1+1)
[1] 2
sum
function (..., na.rm = FALSE)  .Primitive("sum")
#mean is another useful function
mean(c(1,2,3,4))
[1] 2.5

Notice that as you start typing the function, a box pops up in RStudio that prompts you with the arguments you might want to use. Also note that we have to “feed” the mean function a vector of values with c (concatenate) in front of it.

You can convert between data classes using as.class . This is particularly useful for factors. If you have a bunch of character strings, R automatically puts them in alphabetical order, but if you have a factor, you can specify which order the different factor levels should go in.

dog2 <- as.character(dog)
dog2
[1] "24"
class(dog2)
[1] "character"
# This only works if things are compatible
as.numeric(foo)
[1] NA
[1] 24
#let's look at factors
foo3 = as.factor(foo2)
str(foo3)
 Factor w/ 4 levels "Cat","dog","Mouse",..: 3 1 2 4
#You can specify factor order
foo4 <- factor(foo2, levels = c("Squirrel","Mouse", "Cat", "dog"))
str(foo4)
 Factor w/ 4 levels "Squirrel","Mouse",..: 2 3 4 1
#turn it baack into a character
as.character(foo4)
[1] "Mouse"    "Cat"      "dog"      "Squirrel"
#factors are internally stored as numbers, so you can even convert to numeric
as.numeric(foo4)
[1] 2 3 4 1
#you can change the labels
foo5 <- factor(foo2, levels = c("Squirrel","Mouse", "Cat", "dog"),
               labels = c("Wet", "Normal", "Dry", "Critical"))
str(foo5)
 Factor w/ 4 levels "Wet","Normal",..: 2 3 4 1

You can even write your own functions! This often intimidates people at first, but it can be really useful as you get more advanced, since it is a “short cut” that means you can get more done with less typing.

For example, if you wanted to convert a bunch of numbers from Farenheit to Celcius (like we did above), you can write a function that takes a number, subtracts 32, and multiplies by 5/9 all at once.

#we'll name our function FtoC
#it takes a single argument - temperatur ein Farenheit, which we'll call TempF

FtoC <- function(TempF) {
  C <- (TempF-32)*5/9
  return(C) #tell the function what to give you back
}

FtoC(32)
[1] 0
FtoC(c(23,24,56,101))
[1] -5.000000 -4.444444 13.333333 38.333333
FtoC(temps)
[1] 22.22222 20.55556 13.88889 14.44444 21.66667 17.77778 18.33333 21.11111
[9] 15.00000

Don’t worry about writing your own functions too much right now, but I want you to know it’s a possibility.

2.1.4.1 Exercise

See if you can use use these functions

  • mean - calculate the mean of 23, 24, 15, 12, 53, 23, 1, 45

  • sum - calculate the sum of all numbers below 50

    • Hint! You can ask for a sequence of numbers with a : between the first and last number.
  • abs (absolute value)

  • log - default is ln, not log10

  • round - can specify decimal places

  • exp (exponent)

    Click below for the answers when you’re done!

Code
mean(c(23, 24, 15, 12, 53, 23, 1, 45))

sum(c(1:50))

abs(c(-1, 2, -10, 5))

#natural log
log(100)

#log base 10
log(100, base = 10)

#the default for round is no decimal points
round(5.345673)

#but you can specify the number of decimal points with the second argument
round(5.345673, digits = 2)

#this is the same as e^20
exp(20)

exp(log(10))