## File: oop.R ## Author: John Rothfels (rothfels) ## Editted by: Kevin Shin (hgkshin) ## Description: Object Oriented Programming in R ## Lecture 3 Part 5 ## --------------------------------------------- ## The entities R operates on are technically known as objects. To list ## the objects "loaded" in your workspace (e.g. in memory), use 'objects' ## or 'ls'. ls() ## If you need to get rid of objects, use rm() x <- 1:10 ls() rm(x) rm(list = ls()) # removes everything ## Every object has certain intrinsic attributes. Two important intrinsic ## attributes are "mode" and "length". Mode is the "type" of an object, ## namely numeric, complex, logical, character, list, or raw. # Mode is "how R stores the object" vec <- 1:5 mode(vec) strvec <- c(vec, "the components of 'vec' will be coerced to strings") strvec mode(strvec) complexvec <- c(vec, 1+2i) complexvec mode(complexvec) ## Q: What will the following expressions evaluate to? See if you can guess. c(TRUE, 1:5) c(TRUE, "TRUE") c(1, 1+0i, T, "TRUE") ## You can coerce the mode of a vector manually. Any element which cannot ## be coerced into the requested mode will become an NA. mode(vec) <- "complex" vec mode(vec) <- "character" vec mode(vec) <- "numeric" # and back to numeric vec ## Alternatively, you can use the 'as' functions. This will not work on all ## objects; for instance, if you try to cast a certain character vector to ## a numeric vector. This isn't changing the mode, but the class of the object ## (read more below). as.character(0:5) as.complex(0:5) as.logical(0:5) as.numeric(c('a', 'b', 'c')) ## Empty objects still have a mode. e <- numeric() e[3] <- 17 # vector of length 3, first two components are NA ## Just as you can coerce mode, you may also change length length(e) <- 1 # you can extend 'e' similarly e ## Every object belongs to a certain class. So far, we have looked in depth at ## vectors, but there are others. Depending on the mode of the vector, it will ## belong to one of various different classes. # Class is "how R abstracts the object" class(1:5) class(1) # NOTE: difference between 'integer' and 'numeric' class(c('a', 'b', 'c')) class(c(TRUE, FALSE)) class(as.logical(1:5)) ## Here are a few other types of objects we will be looking at in this course. as.matrix(1:5) # class = "matrix" mode(as.matrix(1:5)) # numeric class(as.matrix(1:5)) # matrix as.list(1:5) # class = "list" as.data.frame(1:5) # class = "data frame" but mode = "list" ## The 'is' family of functions can be used to test what class an object ## belongs to. is.matrix(as.matrix(1:5)) is.list(as.matrix(1:5)) ## Every class has certain "attributes". To access these attributes, use the ## 'attributes' function. (Remember, every object has instrinsic attributes ## mode and length). attributes(as.matrix(1:5)) dim(as.matrix(1:5)) attributes(as.data.frame(1:5)) row.names(as.data.frame(1:5)) ## Note that there are ways to create objects of your own class ## and to create your own S3/S4 methods. ## We won't be covering it in 109L since we won't be using it ever ## again in our course, but there are references on the site that ## go into detail about how this can be done. ## This is just a sample of R's object oriented behavior. ## Again, for the purposes of this class, you will not need to know ## how to create classes or write S3/S4 methods.