Menu

Working with Real-World Data in R: Importing and Cleaning Datasets

Working with Real-World Data

Importing CSV and Excel Files

Most real data comes in CSV or Excel files.

  • Use read.csv() or read_csv() from the readr package for CSV files.
  • Use read_excel() from the readxl package for Excel files, and choose the sheet you need.

After you import a file, always check it with head(), str(), and summary() before you start work. If a file does not load, check the file path and the working folder first.

Handling Missing Values and Duplicates

Real data is messy. Some values are missing, shown in R as NA, and some rows are repeated.

  • Find missing values with is.na().
  • Ignore missing values in a calculation by setting na.rm = TRUE.
  • Remove repeated rows with distinct().
  • To fill a missing number, the median is a safe choice because it is not changed by extreme values.

Always note what you changed and why. Count your missing values before and after cleaning to prove that the fix worked.

Transforming and Combining Datasets

Often, your data is in more than one table. A join combines two tables using a shared column, such as an ID.

The left_join() function keeps every row from the first table and adds matching data from the second. Rows without a match show NA.

To change a table between wide and long shapes, use pivot_longer() and pivot_wider() from the tidyr package.

Always check the row count after a join, because a wrong key can add or lose rows.


R for Data Science

HA

Harini A



Learn via Video Course

Data Analysis and Processing with Python using Pandas - Data Science Course Logo

Data Analysis and Processing with Python using Pandas - Data Science Course

5 hrs



Get in Touch For Details