Lab 2
Introduction to R (II)
Basic R Object: Vector
A vector is a sequence of finite elements and serves as the basic object in R. For example, a single number like “3” is considered a vector of length 1. It is a highly relevant object from a statistical perspective, as it allows us to store variables in vector objects. To create a vector, we use the \(\texttt{\color{brown}{c()}}\) function, where the name is an abbreviation of “combine”. For example, suppose we need to create a vector for a set of elements representing weight observations.
As learned, we can assign a descriptive variable name to the vector:
Remark: We can condense the double line process (saving and printing) into a single line by wrapping the assignment process with the \(\texttt{\color{brown}{print()}}\) function.
If we want to create an integer vector, then the \(\texttt{\color{brown}{:}}\) operator or \(\texttt{\color{brown}{seq()}}\) function offer efficient solutions.
Of course, we can combine two vectors by using the \(\texttt{\color{brown}{c()}}\) function again.
Vector Operations in R
One of the powerful features of vectors in R is the ability to perform the same calculation on every element with a single command, i.e., element-wise operation. For example, when converting weights from pounds to kilograms, we can multiply 0.454. This operation can be applied to all the numbers in a vector simultaneously:
The operation between two vectors is also element-wise in R.
Vector Subsetting
To access a specific element in a vector, you add square brackets after the vector name and place the index of the desired element inside the brackets. The “index” refers to the position of the item within the vector, with the first element having an index of 1, and so on. For example, suppose we want to subset the vector \(\texttt{\color{brown}{weight\_pound}}\):
It is also possible to extract multiple elements in a vector.
Lastly, we can exclude particular elements.
Example
Consider the vector \(\texttt{\color{brown}{weight\_update}}\):
- Extract the fifth and sixth elements.
- Extract the last four elemtns.
- Extract all but the last elements.
Length
When the number of elements in a vector (equivalently, the number of observations in a variable, or simply the sample size) is unknown, we can simply use the \(\texttt{\color{brown}{length()}}\) function to obtain this information.
Summation
We can also easily calculate the summation of a vector by using the functions, \(\texttt{\color{brown}{sum()}}\).
Measure of Center
As we discussed in class, we can use the “mean” and “median” to measure the central tendency of our data, with the mode also being an option in some cases. In R, the functions \(\texttt{\color{brown}{mean()}}\) and \(\texttt{\color{brown}{median()}}\) can be used to obtain the mean and median of a vector, respectively.
Example
Compute the mean of \(\texttt{\color{brown}{weight\_kg}}\) by using \(\texttt{\color{brown}{sum()}}\) and \(\texttt{\color{brown}{length()}}\) functions. (Do not use \(\texttt{\color{brown}{mean()}}\) function.)
Lab Questions
- BMI is a measurement of a person’s leanness or corpulence based on their height and weight, and is intended to quantify tissue mass. It is widely used as a general indicator of whether a person has a healthy body weight for their height and it is defined by: \[\textbf{BMI}=703\times \frac{\text{weight (lb)}}{ \text{(height (in)})^2}\] Suppose the previously obtained vector \(\texttt{\color{brown}{weight\_pound}}\) is the weights of seven Lehigh students. Also, the corresponding height observations in inches are \[70,\;72,\;72.5,\;66,\;74,\;66.5,\;72.\]
- Save the height observations in a vector by using a descriptive name.
- Calculate the BMIs of the seven students.
- Compute the mean and median of the BMIs.
- Compute the mean BMI of the first three students and the mean BMI of the last four students.
Click HERE to submit your answers.