Exercise 2
In this practical, you will play around with the different types of R objects.
1 Creating Data Objects
Create two vectors
- One named
vec1containing the integers 1 through 6 - One named
vec2with letters A through F
Create two matrices
- One named
mat1fromvec1 - One named
mat2fromvec2
Define both matrices to have 3 rows and 2 columns.
Inspect vec1, vec2, mat1, and mat2
Are they all numeric?
Make a 6 \(\times\) 2 matrix named mat3 from both vec1 and vec2
- Use
vec1to populate the first column - Use
vec2to populate the second column
Inspect this matrix.
Make a two-column data frame called dat3
- Use function
data.frame(). - Set
vec1andvec2as the columns. - Name the columns
v1andv2, respectively.
2 Data Manipulation
Make a 2-column data frame called dat3b
- Use the
as.data.frame()function with the matrix created in 1 as input. - Set the argument
stringsAsFactors = TRUE. - Name the columns
v1andv2, respectively.
Are the types of the v1 and v2 columns the same as the types of vec1 and vec2, respectively?
Check if the first columns in the data frames from 2 and 3 are numeric
If these columns are not numeric, determine their type.
Select the following elements from the data frame you created in 2
- The third row
- The second column
- The intersection of the above
Inspect the structure of the data frame that you created in 2.
The structure function, str(), allows us to inspect the structure of an R object. Try using it here.
Let’s pretend the first variable (v1) in the data frame you created in 2 (dat3) is not coded correctly, and it actually represents grouping information about cities.
Convert the v1 variable into a factor with the levels:
- Utrecht
- New York,
- London
- Singapore
- Rome
- Cape Town
3 Working with Real Data
Load the workspace boys.RData
You can download the boys.RData workspace here.
Most R packages ship with datasets included (these datasets are most often used for examples to demonstrate the functionality of the package). Since you have not yet learned how to load packages, you get the boys data (which comes from the mice package) as a stand-alone workspace.
View the boys dataset two ways
- By executing
boysin the console - By using the
View()function
Find the dimensions of the boys dataset
Inspect the first 6 cases and the final 6 cases in the dataset
Check if the boys data are sorted on age
Inspect the boys dataset with str()
Use one or more functions to generate numeric summaries of each variable’s distribution
At least show the minimum, the maximum, the mean, and the median for all of the variables.
Give the standard deviations for age and bmi
Tip: Use the help (?) and help search (??) functionality in R, if you get stuck.
4 Logical Subsetting
Create a new data frame containing only the boys that are 20 years old or older
How many boys are at least 20 years old?
Select all boys that are older than 19 but younger than 19.5
How many boys are between the ages of 19 and 19.5?
Compute the mean age of boys younger than 15 years old that do not live in region north
In this exercise, you have learned some basic R usage. The approaches we used for this exercise offer tremendous flexibility but may also be inefficient in complex analyses or data manipulation. Doing advanced operations in basic R can require lots of code. In the next exercise, we will start using packages that allow us to do more complicated operations with fewer lines of code.
As you start using R in your own research, you will quickly find yourself in need of packages that are not part of the default R installation. The beauty of R is that its functionality is community-driven. Anyone can add packages to CRAN, and other people can use and improve these packages. There’s a good chance that a function and/or package has been already developed for the analysis or operation you need. If not, maybe you’re interested in filling the gap by submitting your own package?
End of Exercise 2
—
Copyright Hanne Oberman, 2025 - CC BY-NC-SA 4.0
Materials developed by Amices team - Methodology & Statistics - Utrecht University