1 May, 2017
c()c()
combine or concatenatez <- c(x, y) z
## [1] 2 3
R object to hold more than one value:
Rsqrt(z) 2^z length(z)
Hint: What types of values exist?
logical (TRUE or FALSE)integernumeric (or double): Numbers with decimal pointscharacterraw (bytes) and complex (\(i =\sqrt{-1}\))z > 0
## [1] TRUE TRUE
z == 2
## [1] TRUE FALSE
logical vectors laterWe can coerce some types of values into another type
typeof(z) as.logical(z) as.integer(z) as.character(z)
Note that as you start typing the name of the function, RStudio will start to guess what you want
This is known as auto-completion
character vectorshomeR <- c(742, "Evergreen", "Terrace") length(homeR)
## [1] 3
typeof(homeR)
## [1] "character"
integer, double or character?character vectorsCan we convert this to any of the other vector types?
as.numeric(homeR) as.logical(homeR)
Try it and see…
character vectorsCan we convert this to any of the other vector types?
as.numeric(homeR) as.logical(homeR)
Warning message: NAs introduced by coercion make any sense?R was able to coerce "742" to a numeric valueR was unable to coerce the other words (for obvious reasons)There is a trap for young players…
R would traditionally convert text to categorical variables (factors)integer specifying which category the value is from
level)Automatic conversion is not as common now, but important to know about!
groups <- c("A", "A", "B")
groups <- as.factor(groups)
groups
## [1] A A B ## Levels: A B
Now prepare to be amazed…
typeof(groups) levels(groups) as.integer(groups) as.logical(groups)
Before the next section:
Environment TabYes to the question "Are you sure…"We've just deleted all our R objects and have a clean Environment