Data Science with R
  • Syllabus
  • Lecture Notes
  • Assignments
  • Exercises

On this page

  • 1 Exercise 1
    • 1.1 Problem 1
    • 1.2 Problem 2
  • 2 Exercise 2
    • 2.1 Problem 1
    • 2.2 Problem 2

Ex-1-3: Reshape Datasets

1 Exercise 1

1.1 Problem 1

Datasets: data_long synthetically generated withint R below.

Task: Reshape the data_long dataset to wide format, where each year becomes a separate column. using tidyr::pivor_wider().

Load and Create Dataset:

  • Work here
  • Answer

Here is the output of successfully reshaped data:

1.2 Problem 2

Datasets: data_wide (data_long converted to wide format)

Task: Reshape the data_wide dataset to long format using tidyr::pivor_longer(). In the new dataset,

  • name the column that will hold the current column names year.
  • name the column that will hold the values value.

Load and Create Dataset:

  • Work here
  • Answer

Here is the output of successfully reshaped data:

2 Exercise 2

2.1 Problem 1

Datasets: crop_data synthetically generated withint R.

Task: Reshape the economics dataset to wide format using tidyr::pivor_wider(). In doing so, append yield_ to the new column names using the names_prefix option.

Load and Create Dataset:

  • Work here
  • Answer

Here is the output of successfully reshaped data:

2.2 Problem 2

Datasets: the crop_data_wide (crop_data in wide format)

Task: Reshape the economics dataset to long format using tidyr::pivor_longer(). In doing so, remove yield_ from the column names using the names_prefix option.

Load and Create Dataset:

  • Work here
  • Answer

Here is the output of successfully reshaped data: