📖 Scenario: You work in a small library. You have a list of books where the author and title are combined in one column. You want to split them into two columns for better organization. Later, you want to join them back into one column.
🎯 Goal: Learn how to use separate() to split one column into two, and unite() to join two columns back into one in R.
📋 What You'll Learn
Create a data frame called books with a column author_title containing exact values
Create a separator variable called sep_char with the exact value used in the data
Use separate() to split author_title into author and title columns
Use unite() to combine author and title back into author_title
Print the final data frame
💡 Why This Matters
🌍 Real World
Libraries, shops, and many businesses often have data combined in one column that needs to be split for better analysis or reporting.
💼 Career
Data analysts and scientists frequently clean and organize data using functions like separate and unite to prepare data for insights.
Progress0 / 4 steps
1
Create the initial data frame
Create a data frame called books with one column author_title containing these exact values: 'Rowling - Harry Potter', 'Tolkien - Lord of the Rings', 'Lewis - Narnia'.
R Programming
Hint
Use data.frame() and c() to create the column with exact strings.
2
Create the separator variable
Create a variable called sep_char and set it to the string ' - ' which is the separator between author and title in author_title.
R Programming
Hint
Assign the exact string ' - ' to sep_char.
3
Separate the author and title columns
Use separate() from the tidyr package to split the author_title column in books into two new columns: author and title. Use the separator stored in sep_char. Save the result back into books.
R Programming
Hint
Use separate(books, col = author_title, into = c('author', 'title'), sep = sep_char).
4
Unite the columns and print the result
Use unite() from tidyr to combine the author and title columns back into one column called author_title, separated by sep_char. Save the result back into books. Then print books.
R Programming
Hint
Use unite(books, col = author_title, author, title, sep = sep_char) and then print(books).
Practice
(1/5)
1. What does the separate function do in R's tidyr package?
easy
A. It removes missing values from a dataset.
B. It combines multiple columns into one column.
C. It splits one column into multiple columns based on a separator.
D. It sorts the rows of a data frame.
Solution
Step 1: Understand the purpose of separate
The separate function is designed to split a single column into multiple columns by using a separator like a comma or space.
Step 2: Compare with other options
unite does the opposite by joining columns, and other options do unrelated tasks.
Final Answer:
It splits one column into multiple columns based on a separator. -> Option C
Quick Check:
separate splits columns [OK]
Hint: Remember: separate = split one column into many [OK]
Common Mistakes:
Confusing separate with unite
Thinking separate removes rows
Assuming separate combines columns
2. Which of the following is the correct syntax to unite columns first and last into a new column full_name using tidyr?
easy
A. unite(data, full_name, first & last, sep = " ")
B. unite(data, first, last, full_name, sep = " ")
C. unite(data, full_name, first + last, sep = " ")
D. unite(data, full_name, first, last, sep = " ")
Solution
Step 1: Recall unite function syntax
The correct syntax is unite(data, new_column, col1, col2, sep = "separator").
Step 2: Match options with syntax
unite(data, full_name, first, last, sep = " ") matches the syntax exactly, specifying the data, new column name, columns to unite, and separator.
Final Answer:
unite(data, full_name, first, last, sep = " ") -> Option D
Quick Check:
unite syntax = unite(data, new_col, cols...) [OK]
Hint: unite(data, new_col, col1, col2, sep) is correct [OK]
Common Mistakes:
Mixing order of arguments
Using + instead of commas
Passing columns as a vector incorrectly
3. Given the data frame df with a column date containing values like "2024-06-15", what will be the result of separate(df, date, into = c("year", "month", "day"), sep = "-")?
medium
A. A data frame with columns year, month, day split from the date string.
B. An error because sep must be a space.
C. The original data frame unchanged.
D. A single column with dates joined by dashes.
Solution
Step 1: Understand separate with sep = "-"
The function splits the date column at each dash, creating three new columns named year, month, and day.
Step 2: Confirm output structure
The output will be a data frame with these three new columns containing parts of the original date string.
Final Answer:
A data frame with columns year, month, day split from the date string. -> Option A
Quick Check:
separate splits by given separator [OK]
Hint: sep argument splits string at given character [OK]
Common Mistakes:
Assuming sep must be space
Expecting a single combined column
Not naming the new columns correctly
4. What is wrong with this code snippet?
unite(full_name, first, last)
It produces an error.
medium
A. The data frame df is not specified.
B. Missing the separator argument sep.
C. The columns first and last must be in a vector.
D. The new column name full_name must be quoted.
Solution
Step 1: Check unite function syntax
The unite function requires the data frame as the first argument.
Step 2: Identify the issue
In the code, full_name is the first argument, which is the new column name, not a data frame. The data frame (e.g., df) is missing.
Final Answer:
The data frame df is not specified. -> Option A
Quick Check:
unite(dataframe, new_col, cols...) [OK]
Hint: Data frame must be first argument in unite [OK]
Common Mistakes:
Forgetting to pass data frame first
Not quoting new column name
Passing columns as vector incorrectly
5. You have a data frame df with columns location containing "city,state" strings. How do you split location into two columns city and state, then combine them back into location separated by a comma and space?
hard
A. Use unite(df, location, city, state, sep = ",") then separate(df, location, into = c("city", "state"), sep = ", ").
B. Use separate(df, location, into = c("city", "state"), sep = ",") then unite(df, location, city, state, sep = ", ").
C. Use separate(df, location, into = c("city", "state"), sep = ", ") then unite(df, location, city, state, sep = ",").
D. Use separate(df, location, into = c("city", "state"), sep = ",") then unite(df, location, city, state, sep = ",").
Solution
Step 1: Separate location by comma
Use separate with sep = "," to split "city,state" into two columns.
Step 2: Unite city and state with comma and space
Use unite with sep = ", " to join city and state back with a comma and space.
Final Answer:
Use separate with sep="," then unite with sep=", " -> Option B
Quick Check:
Separate splits, unite joins with correct separators [OK]
Hint: Match separators exactly when separating and uniting [OK]
Common Mistakes:
Mixing separator strings between separate and unite
Using wrong order of functions
Not matching spaces in separators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.