20  Data Manipulation and Reshaping

Once a dataset has been imported and cleaned (see Data Import and Cleaning), the next step is usually to reshape it — selecting the columns that matter, filtering to the rows of interest, and switching between wide and long layouts depending on what a chart or model needs. This topic continues with the clean farm-records data frame built in the previous topic.

20.1 Data Manipulation with dplyr

The dplyr package provides functions for filtering, selecting, modifying, and restructuring data using a small, consistent set of verbs — select(), filter(), arrange(), and others — that chain together with the pipe operator %>%.

20.1.1 Install the dplyr Package

Code

20.1.2 Selecting Specific Columns

20.1.3 Removing Columns

20.1.4 Filtering Data

20.1.5 Sorting Data


20.2 Reshaping Data with tidyr

tidyr reshapes data between wide format (one column per variable) and long format (one row per observation) — long format is often what plotting and modeling functions expect.

20.2.1 Converting Wide Data to Long Format

20.2.2 Converting Long Data to Wide Format


20.3 Exporting Data from R

20.3.1 Export the Data as a CSV File

The write.csv() function exports a data frame to a Comma-Separated Values (CSV) file, making it easy to save, share, and open the data in other tools like Excel, Python, or SQL.

write.csv(data, "folder path/filename.csv") — provide a path to save the file in a specific folder.

20.3.2 Export the Data as an Excel File

writexl: Writing Data to Excel Files

The writexl package provides an easy way to export data from R into an Excel file without requiring external dependencies.

Key features:

  • Writes .xlsx files quickly.
  • Preserves column types and formats.

Install the writexl Package

Code
install writexl package
install.packages("writexl")

Export the cleaned data file as an .xlsx file:

Code
library(writexl)
# Write data to an Excel file
write_xlsx(clean, "clean_farm_data.xlsx")

20.3.3 Check the List of Files in the Directory (or Project)

Code
check the list of files in the directory

Summary

Concept Description
Data Manipulation with dplyr
Data Manipulation with `dplyr` dplyr provides a consistent set of verbs — select(), filter(), arrange() — for filtering, selecting, modifying, and restructuring data
Selecting Specific Columns select() keeps only the named columns
Removing Columns select(-column) drops a named column
Filtering Data filter() keeps rows that meet a logical condition
Sorting Data arrange(desc(column)) sorts rows by a column, highest first
Reshaping Data with tidyr
Reshaping Data with `tidyr` tidyr reshapes data between wide (one column per variable) and long (one row per observation) formats
Wide to Long Format pivot_longer() collapses several columns into key-value pairs
Long to Wide Format pivot_wider() spreads key-value pairs back into separate columns
Exporting Data from R
Exporting Data from R write.csv() and write_xlsx() save a data frame to a file others can open
Export as CSV write.csv(data, "file.csv") saves a CSV file
Export as Excel write_xlsx(data, "file.xlsx") saves an Excel file