Code
install.packages("dplyr")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.
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 %>%.
install.packages("dplyr")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.
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.
writexl: Writing Data to Excel FilesThe writexl package provides an easy way to export data from R into an Excel file without requiring external dependencies.
Key features:
.xlsx files quickly.Export the cleaned data file as an .xlsx file:
library(writexl)
# Write data to an Excel file
write_xlsx(clean, "clean_farm_data.xlsx")| 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 |