R Programming
- Overview of R
- Installing R on Windows
- Download and Install RStudio on Windows
- Setting Your Working Directory (Windows)
- Getting Help with R
- Installing R Packages
- Loading R Packages
- Take Input and Print in R
- R Objects and Attributes
- R Data Structures
- R – Operators
- Vectorization
- Dates and Times
- Data Summary
- Reading and Writing Data to and from R
- Control Structure
- Loop Functions
- Functions
- Data Frames and dplyr Package
- Generating Random Numbers
- Random Number Seed in R
- Random Sampling
- Data Visualization Using R
Mixing Objects
There are occasions when different classes of R objects get mixed together. You already know that vectors must contains elements that are all same type, R will automatically coerce to a single type when attempting to create a vector that combines multiple types.
x<- c(100, "Statistics with R", TRUE) #character
y <- c(TRUE, 200) #numeric
z <- c("a", TRUE) # characterclass(x)
class(y)
class(z)
Output:
> class(x)
[1] "character"
> class(y)
[1] "numeric"
> class(z)
[1] "character"
Remember that the only rule about vectors says this is not allowed. When different objects are mixed in a vector, coercion occurs so that every element in the vector is of the same class.