Chapter3

# Chapter3 - Chapter 3 R Chapter 3 3.1 R Objects Introduction...

This preview shows pages 1–4. Sign up to view the full content.

Chapter 3 R 1 Chapter 3 R Objects 3.1 Introduction of R Objects The entities R operates on are technically known as objects. Examples are vectors of numeric (real) or complex values, vectors of logical values and vectors of character strings. These are known as “atomic” structures since their components are all of the same type, or mode, namely numeric, complex, logical, character and raw. Use the is() command to determine what an object is. 1. Mode of Objects Vectors must have their values all of the same mode. Thus any given vector must be unambiguously either logical, numeric, complex, character or raw. a. Numeric is the default value type for most numbers. An integer is a subset of the numeric class, and may be used as a numeric value. You can perform any type of math or logical operation on numeric values, including: > log(3 * 4 * (2 + pi)) # Note that pi is a built-in constant, [1] 4.122270 # and log() the natural log function. > 2 > 3 # Basic logical operations, including >, [1] FALSE # <, >= (greater than or equals), # <= (less than or equals), == (exactly # equals), and != (not equals). > 3 >= 2 && 100 == 1000/10 # Advanced logical operations, including [1] TRUE # & (and), && (if and only if), | (or), # and || (either or). Note that Inf (infinity), -Inf (negative infinity), NA (missing value), and NaN (not a number) are special numeric values on which most math operations will fail. (Logical operations will work, however.)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 3 R 2 b. Logical operations create logical values of either TRUE or FALSE. To convert logical values to numerical values, use the as.integer() command: > as.integer(TRUE) [1] 1 > as.integer(FALSE) [1] 0 c. Character values are text strings. For example, > text <- "Georgia Bulldog" > text [1] "Georgia Bulldog" assigns the text string on the right-hand side of the <- to the named object in your workspace. d. Note that a vector can be empty and still have a mode. For example the empty character string vector is listed as character(0) and the empty numeric vector as numeric(0). 2. Length of Objects By the mode of an object we mean the basic type of its fundamental constituents. This is a special case of a “property” of an object. Another property of every object is its length. The functions mode(object) and length(object) can be used to find out the mode and length of any defined structure. Changing the length of an object An “ empty ” object may still have a mode. For example
Chapter 3 R 3 > e <- numeric() makes e an empty vector structure of mode numeric. Similarly character() is a empty character vector, and so on. Once an object of any size has been created, new components may be added to it simply by giving it an index value outside its previous range. Thus > e[3] <- 17 now makes e a vector of length 3, (the first two components of which are at this point both NA). This applies to any structure at all, provided the mode of the additional component(s) agrees with

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 15

Chapter3 - Chapter 3 R Chapter 3 3.1 R Objects Introduction...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online