# 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)
- Create sample data using
data.frame
, assigning it to a variable named data
. - 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. - Combine the split names with the original data using
cbind
, assigning the result back to the data
variable. - Rename the newly created columns 'X1' and 'X2' to 'first_name' and 'last_name' using
colnames
. - Display the updated data using
print
.