#Load train.csv data file

file_path <- “C:\Users\fardm\OneDrive\Desktop\PhD\train.csv” train_data <- read.csv(“C:\Users\fardm\OneDrive\Desktop\PhD\train.csv”)

test_data < # Example of feaature engineering - standardizing a variable mean_age <- mean (train_data\(Age, na.rm = TRUE) sd_age <- sd(train_data\)Age, na.rm = TRUE)

train_data\(Age_scaled <- (train_data\)Age - mean_age) / sd_age test_data\(Age_scaled <- (test_data\)Age - mean_age) / sd_age

Extract a New Variable from an Existing Variable

This code uses regular expressions to extract titles from names. It assumes the format “Last, Title. First”.

train_set\(Title <- gsub('(.*, )|(\\..*)', '', train_set\)Name) test_set\(Title <- gsub('(.*, )|(\\..*)', '', test_set\)Name)

Create a Quantitative Mixture of Variables

Create a new feature by combining “Age” and “Spa” expenditure

train_set\(AgeSpaInteraction <- train_set\)Age * train_set\(Spa test_set\)AgeSpaInteraction <- test_set\(Age * test_set\)Spa

Collapse a Factor Level Variable

collapse_destination <- function(destination) { if (destination %in% c(“Earth”, “Mars”)) { return(“Inner Solar System”) } else if (destination == “Europa”) { return(“Outer Solar System”) } else { return(destination) # For any destination not explicitly mentioned } }

Apply the function to the dataset

train_set\(CollapsedDestination <- sapply(train_set\)Destination, collapse_destination) test_set\(CollapsedDestination <- sapply(test_set\)Destination, collapse_destination)