how to split a column in r

# Sample data
data <- data.frame(full_name = c("John Doe", "Jane Smith", "Alan Johnson"))

# Splitting the 'full_name' column into first and last names
data <- cbind(data, strsplit(as.character(data$full_name), " ", fixed = TRUE))

# Renaming the columns
colnames(data)[3:4] <- c("first_name", "last_name")

# Displaying the updated data
print(data)
  1. Create sample data using data.frame, assigning it to a variable named data.
  2. Use strsplit to split the 'full_name' column into separate elements based on the space character. The as.character function converts the 'full_name' column to character type, and fixed = TRUE ensures that the splitting occurs on exact matches of the space character.
  3. Combine the split names with the original data using cbind, assigning the result back to the data variable.
  4. Rename the newly created columns 'X1' and 'X2' to 'first_name' and 'last_name' using colnames.
  5. Display the updated data using print.