R function train test split

R function train test split

Function to convert a Dataframe into Train Test Split in R

get_train_test_split <- function(df, split = 0.8, seed = 123) {
  set.seed(seed)  

  n_rows <- nrow(df)
  train_indices <- sample(1:n_rows, size = floor(split * n_rows))

  train_df <- df[train_indices, ]
  test_df  <- df[-train_indices, ]

  return(list(train = train_df, test = test_df))
}

rv = get_train_test_split(titanic)