Importing data to R

Importing a simple data set to R

As part of a study of 25 adult sheep, the resting pulmonary ventilation rate in L min-1 was measured for each animal. The data were recorded on paper and ultimately published in a report. Unfortunately, the original data sheets where the data were recorded were lost, and the only electronic copy was held by a colleague who has since retired and can longer locate the data among their files. The only remaining record of the data is in the report. The image below was taken of the table of data in the report, and shows the measurements of the pulmonary ventilation rate (L min-1) in the 25 adult sheep.

NoteTask

Create a vector in R containing these measurements. Name your vector sheep. You should enter the data row-wise; when creating the vector, enter the first row of values, then the second, and so on.

Use the c() function to create the vector.

You create an atomic vector using the c() function, and provide the data values are arguments to that function, separated by ,. Here, I enter each row of data from the image as a separate row in the code; this makes it easier to spot mistakes, and the code more closely resembles the image of the data.

sheep <- c(
  8.3, 8.0, 9.9, 6.1, 5.5,
  10.3, .5, 7.6, 7.6, 7.6,
  6.9, 10.3, 7.8, 7.3, 8.9,
  10.1, 7.6, 9.1, 8.3, 4.8,
  10.2, 6.5, 9.1, 7.0, 11.0
)

Complete the following tasks using the imported sheep data.

NoteTask

What is the pulmonary ventilation rate of the 17th sheep?

The pulmonary ventilation rate of the 17th sheep is 7.6 L min-1.

sheep[17]
[1] 7.6
NoteTask

What is the lowest pulmonary ventilation rate observed among the 25 sheep?

The lowest pulmonary ventilation rate of the 25 sheep is 0.5 L min-1.

min(sheep)
[1] 0.5
NoteTask

What is the highest pulmonary ventilation rate observed among the 25 sheep?

The highest pulmonary ventilation rate of the 25 sheep is 11 L min-1.

max(sheep)
[1] 11
NoteTask

The report notes that the first three rows of data are from female sheep, while the last two rows are for male sheep.

Create a vector sex that contains this information. Use female and male as the data values in the vector.

The aim of this task is to create the required vector without having to repeatedly enter or copy/paste these values into a vector. Think about how you can create the vector without having to repeatedly enter female and male?

The easiest way to create this vector is using rep().

We can create the required vector using

sex <- rep(c("female", "male"), times = c(15, 10))
NoteTask

The report also notes that the data are for different breeds of sheep:

  • the first and fourth rows of data are for the Merino breed,
  • the second and fifth rows of data are for the Cheviot breed, and
  • the third row of data is for the East Friesian breed.

Create a vector breed that contains this information. Use Merino, Cheviot, and East Friesian as the data values in the vector.

We can create the required vector using

breeds <- c("Merino", "Cheviot", "East Friesian")
breed <- rep(breeds, each = 5) |>
  rep(length.out = 25)

# this is the same as
breed <- rep(rep(breeds, each = 5), length.out = 25)

Notice that the breeds are in the same order within the female and male sheep, except that the male sheep are not from the East Friesian breed. So, I can create a vector of breeds for the first 15 sheep (the females) using rep(breeds, each = 5) and then I use the length.out trick to repeat that vector until we get to the required length.

An alternative way to solve this would be

breeds <- c("Merino", "Cheviot", "East Friesian", "Merino", "Cheviot")
breed <- rep(breeds, times = rep(5, length(breeds)))

Either way is acceptable.

NoteTask

Combine the three vectors

  • sheep,
  • sex, and
  • breed

into a single data frame named sheep_data, where the variable names are

  • pulmonary
  • sex, and
  • breed.
sheep_data <- data.frame(
  pulmonary = sheep,
  sex = sex,
  breed = breed
)

# we can view the first few rows using the `head()` function
head(sheep_data)
  pulmonary    sex   breed
1       8.3 female  Merino
2       8.0 female  Merino
3       9.9 female  Merino
4       6.1 female  Merino
5       5.5 female  Merino
6      10.3 female Cheviot

Importing data files into R

In this activity, you will use the readr and readxl packages to load two different data sets into R.

Hansi and Apricot’s weight data

The complete records of Hansi’s and Apricot’s weight recordings is in the file data/hansi-apricot-weights.csv

NoteQuestion

Which package, readr or readxl will be needed to read these weight data into R?

The data are in a comma separated value format, as indicated by the .csv extension. The readr package will be needed to read this data into R. The readxl package only hands data stored in Excel workbooks, with extensions .xls and .xlsx.

NoteTask

Load the needed package into R so you can use it.

We load packages into our R session using the library() function.

library("readr")
NoteTask

Import the data file containing Hansi’s and Apricot’s weight recordings. Import the data into a data frame named cat_weights

We use the read_csv() function to read in CSV files

cat_weights <- read_csv(
  "data/hansi-apricot-weights.csv"
)
Rows: 31 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (2): Hansi, Apricot

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
NoteTask

How would you import the data to avoid the messages from read_csv()?

We use the read_csv() function to read in CSV files

cat_weights <- read_csv(
  "data/hansi-apricot-weights.csv",
  col_types = "dd"
)

Sheep nociception data

As part of a research project to study the relationship between pain thresholds and the incidence of foot rot, control data on pain thresholds was obtained through the application og a mechanical stimulus to the forelimb of sheep in the study.

The data are provided here in the file data/sheep-nociception.xls.

We will use the readxl package to read these data into R. As the data are stored in the older, binary .xls format, we should use the read_xls() function to read the data into R.

NoteTask

Read the sheep nociception data into R using the read_xls() function. Import the data into an object name pain_thresholds.

Make sure you load the readxl package into R first, before you try to use it.

library("readxl")
pain_thresholds <- read_xls(
  "data/sheep-nociception.xls"
)
NoteTask

Once you have loaded the data into R, look at it and answer the following questions:

  1. How many observations are in the data set?
  2. How many variables are in the data set?
  3. What are the names of the variables in the data set?

The answers to the questions are

  1. There are 470 observations in the data set.
  2. There is 1 variable in the data set.
  3. The name of the variable is threshold.

You could have found this out by printing the object

pain_thresholds
# A tibble: 470 × 1
   threshold
       <dbl>
 1       1  
 2       1  
 3       1.1
 4       1.4
 5       1.4
 6       1.4
 7       1.5
 8       1.7
 9       1.9
10       2  
# ℹ 460 more rows

and noting what is displayed.

A more programmatic way of finding this information out is using functions:

# the number of observations (rows)
nrow(pain_thresholds)
[1] 470
# the number of variables (columns)
ncol(pain_thresholds)
[1] 1
# the name(s) of the variables
names(pain_thresholds)
[1] "threshold"

Weights of chicken hearts

In September 2024, the first cohort of vet students to be trained at AU Viborg did a laboratory dissection exercise as part of which they observed the mass of a ventricle from the heart of a chicken. The students were asked to weigh the ventricle a number of times, recording the time of observation and the mass of the ventricle at each observation time. The students worked in pairs, and some pairs had time for both students in the pair to make their own observations of the mass of the ventricle. In addition to the mass data, some other information about each chicken was recorded. Each pair recorded their observations on a data sheet.

Unfortunately, we have misplaced the data sheets, but we do have a print out of the data that was compiled from the individual data sheets.

NoteTask

Your task is to enter the data from the print out into an Excel worksheet (using Excel or Google Sheets for example), and then import the data into R.

In your groups, decide how you should arrange the data in the file you are creating.

Enter the data and then save the file to your computer. Upload this file to Posit.cloud using the Upload button in the Files pane in RStudio.

Finally, import the data set you created into R, into an object named chicken_mass.

You will need to troubleshoot any errors you get arising from the way you entered the data into the spreadsheet.

Remember the tips from the video to help you.