############################################################ ### R BASICS WORKSHOP ### ### PRESENTATION 3.1: CREATING OBJECTS AND TYPES OF DATA ### ### ### ### Center for Conservation and Sustainable Development ### ### Missouri Botanical Garden ### ### Website: rbasicsworkshop.weebly.com ### ### Last modification: 2021-June-06 ### ############################################################ ### A. WHAT AN OBJECT IS AND HOW TO CREATE IT ############## # Simply, an object in R is a piece of memory with a name. # Objects contains some information, which is often used for # figures or analyses. # Objects can have multiple types of data and can be of # various classes # The easiest way to create an object is using the arrow # operator *<-* x <- 10 # This creates an object called *x* that contains # the value 10 x # Typing the object name returns the information stored # inside the object by printing it in the console y <- 5.3 # This created another object called *y* that # contains the value 5.3 y # The arrow can also be pointing in the opposite direction 5.3 -> z z # It is often useful to store the output of a function in # an object: seq(from = 4, to = 10, by = 2) # This just prints the # result in the screen x <- seq(from=4, to=10, by=2) # This RE-WRITES the object # *x* with the sequence x # Now the object *x* contains the sequence from 4 to 10 by 2 # Other examples: b <- 1.4 x <- rnorm(100, 4, 2) e <- rnorm (100, 0, 1) y <- 4 + b*x + e # Now the objects above are used to create one more object plot(y ~ x) abline(lm(y ~ x)) # Now the objects we created are used to make a scatterplot # with a regression line ### B. TYPES OF DATA WITHIN OBJECTS ######################## # There are 4 types of data commonly used in R: # 1. Numerical - Numbers - e.g. 5, 0.4, 3.885 # 2. Characters - text - e.g. "a", "workshop", "R is awesome" # 3. Logical - i.e. TRUE, FALSE # 4. Special Values - e.g. NA, Inf, NULL, NaN ## 1-3. NUMERIC, CHARACTER AND LOGIC VALUES ## # a. Let's suppose we have abundances of 5 species measured # in a forest plot in two years (numeric values). abund.t1 <- c(0, 17, 34, 26, 82) abund.t2 <- c(1, 17, 31, 27, 91) # b. Let's suppose, we also have the names of the species # (character data). spp <- c("I.ynga", "I.edulis", "I.macrophylla", "I.punctata", "I.alba") # c. Also, let's suppose we need a variable that indicate # if abundances have increased or not (logical values). increm <- c(TRUE, FALSE, FALSE, TRUE, TRUE) abund.t1 abund.t2 spp increm # The function *mode* can be used to return the type of data # inside an object. mode(x=abund.t1) mode(x=abund.t2) mode(x=spp) mode(x=increm) # IMPORTANT: Note that character values NEED to be between # quotation marks. If not, R thinks you are using an object # with that name. Thus, this will generate an error: spp.2 <- c(I.ynga, I.edulis, I.macrophylla, I.punctata, I.alba) # IMPORTANT: Note that logical values MUST NOT be between # quotation marks. If they are, R will interpret them as # characters: increm.2 <- c("TRUE", "FALSE", "FALSE", "TRUE", "TRUE") mode(increm.2) identical(increm, increm.2) ## 4. SPECIAL VALUES ## # There are multiple types of special values, but the most # common are NA and Inf # 4.1. NA: missing value (not available) x <- NA x length(x) is.na(x) # 4.2. Inf: infinite 100/0 -100/0 100-Inf