1. Objects - the building blocks of R
As you begin chapter 2, you will learn how to create and manipulate objects, which are the building blocks of R.
2. Objects are the building blocks of R
Everything in R is an object
from single numbers to
vectors,
matrices,
datasets and
output from functions.
3. Work with single elements
To create an object in R you type the name of the object followed by the assign operator which is the less than symbol followed by a dash.
Here the value of 4 is assigned to an object named x.
To view the content in X, type X and hit the enter key.
The 1 between brackets in the output indicates that object X has 1 value in it.
X is now in the global environment. You can use X to create y assigned to the result of x squared which is the value 16.
4. Other types of elements
In addition to numbers, objects can contain characters or words like fish.
They can also be logical and contain the values of TRUE or FALSE.
5. Combine single elements into a vector
Larger objects can be created by combining values using the c combine function.
6. Combine single elements into a vector
A numeric vector with the 3 numbers 5, 3, 2 can be created using the c function
and then assigning the result to object x.
7. Create vector of character elements
Character elements are defined by double quotes around the word
child shown here.
8. Create vector of character elements
The second character element
is the word young.
9. Create vector of character elements
The third element is the word old.
These 3 words are combined into character vector y.
10. Create vector of logical elements
Create a logical vector with first element TRUE.
Logical vectors are created with elements TRUE or FALSE written in all caps without double quotes. Capital T or F can also be used.
11. Create vector of logical elements
Add second element FALSE to logical vector z.
12. Create vector of logical elements
Add third element TRUE to logical vector z and view object z.
13. Create matrix from vectors
Matrices are created from vectors
of the same type and same length.
Let the first vector a be numeric
with 3 elements.
14. Create matrix from vectors
Create a second numeric vector B with 3 elements.
15. Create matrix from vectors
Matrix m is created using the matrix function.
The first argument defines the data to include - the numeric vectors A and B.
Next the number of rows and columns are defined by nrow and ncol.
The output shows the matrix elements indexed by row and column.
16. Create data frame from vectors
Data frames are created like matrices
by combining vectors of the same length.
However data frames can have vectors of different types.
First, define a numeric vector called score.
17. Create data frame from vectors
Next define a character vector called age.
18. Create data frame from vectors
Then define a logical vector called test.
19. Create data frame from vectors
So, the dataframe d is created using the data.frame function
to combine the score numeric vector, age character vector and test logical vector together as columns in d.
20. Determine object type
After creating an object like the numeric vector x, the object type can be determined by running the class function.
x is confirmed to be numeric.
The str structure function also shows that x is a numeric vector with 3 elements.
21. Determine object type
The class function confirms that y is a character vector and z is a logical vector.
The str structure function also shows that y is character type and z is logical type, both vectors with 3 elements.
22. Determine object type
For the matrix m
the class function confirms m is a matrix.
The str structure function shows that m is numeric with two dimensions of 3 rows and 2 columns.
23. Determine object type
The class function confirms that d
is a dataframe object.
str also shows that d is a data frame with 3 observations and 3 variables,
The str output also shows each variable's name, class type, and the first few elements for each.
24. Let's create and manipulate data objects in R
So, let's create and manipulate data objects in R.