Question
mixed.1
mixed.1 <- c(TRUE,TRUE,FALSE,4,0,3)
mixed.1
## [1] 1 1 0 4 0 3
mixed.2 <- c(c(TRUE,TRUE,FALSE,4,0),"3")
mixed.2
## [1] "1" "1" "0" "4" "0" "3"
mixed.3 <- c(TRUE,TRUE,FALSE,4,0,"3")
mixed.3
Type Casting: The as.logical(), as.numeric(), and as.character() functions allow us to coerce (or cast) a vector into one of a different mode. For example:
as.logical(c("TRUE","FALSE","TRUE"))
## [1] TRUE FALSE TRUE
as.numeric(c("4","0","3"))
## [1] 4 0 3
as.character(c(TRUE,FALSE,TRUE))
## [1] "TRUE" "FALSE" "TRUE"
(a) Explain why as.numeric(mixed.2) and as.numeric(mixed.3) produce different results.
(b) Explain why as.logical(mixed.2) and as.logical(mixed.3) produce different results.
(c) How could you use the type casting functions to coerce mixed.2 into a meaningful logical vector?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started