Member-only story

R for Data Science: Quick 100 One-liner Codes

btd
4 min readNov 28, 2023

--

Navigating the world of machine learning often involves dealing with diverse datasets, and effective data preprocessing is crucial. In this curated list, you’ll find 100 concise R codes to streamline data cleaning, exploratory analysis, and statistical operations.

Data Loading and Inspection

  1. Load Data: data <- read.csv('file.csv')
  2. View Data Structure: str(data)
  3. Summary Statistics: summary(data)

Handling Missing Values

  1. Handle Missing Values: na.omit(data)
  2. Impute Missing Values: data$variable[is.na(data$variable)] <- mean(data$variable, na.rm = TRUE)

Data Cleaning and Transformation

  1. Remove Duplicates: unique_data <- unique(data)
  2. Subset Data: subset_data <- data[data$condition == 'value', ]
  3. Rename Columns: colnames(data) <- c('new_name', 'new_name2')
  4. Convert Factor to Numeric: data$variable <- as.numeric(as.character(data$variable))
  5. Convert Date: data$date <- as.Date(data$date, format='%Y-%m-%d')
  6. Convert Character to Factor: data$variable <- as.factor(data$variable)

--

--

btd
btd

No responses yet