Can't unite columns in R

Hi,

I am a newbie to R.

I was trying to unite columns in R using the unite(employee,'name',first_name,last_name,sep=' ') but got the following:
Error in chr_as_locations():
! Can't subset columns that don't exist.
x Column first_name doesn't exist.
Run rlang::last_error() to see where the error occurred.

Please how should I resolve this?

Hi, welcome!

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

1 Like

Thank you nirgrahamuk,

This is my data frame which was manually done:

id first_name last_name job_title
1 1 John Mendes Professional
2 2 Rob Stewart Programmer
3 3 Rachel Abrahamson Management
4 4 Christy Hickman Clerical
5 5 Johnson Harper Developer
6 6 Candace Miller Programmer
7 7 Carlson Landy Management
8 8 Pansy Jordan Clerical
9 9 Darius Berry Developer
10 10 Claudia Garcia Programmer

These are the variables and their vectors:

id <- c(1:10)
name <- c("John Mendes", "Rob Stewart", "Rachel Abrahamson", "Christy Hickman", "Johnson Harper", "Candace Miller", "Carlson Landy", "Pansy Jordan", "Darius Berry", "Claudia Garcia")
job_title <- c("Professional", "Programmer", "Management", "Clerical", "Developer", "Programmer", "Management", "Clerical", "Developer", "Programmer")

This is the data frame code chunk:

employee <- data.frame(id,name,job_title)

I had originally separated the name column into first name and last name columns. Then I wanted to unite the first name and last name columns into a single name column and that was when I encountered the error I posted inititally.

I can't reproduce your issue, the code works as expected with your sample data, and you are not actually showing a reproducible example of your issue.

library(dplyr)
library(tidyr)

employee <- data.frame(
  stringsAsFactors = FALSE,
                id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
        first_name = c("John","Rob","Rachel",
                       "Christy","Johnson","Candace","Carlson","Pansy","Darius",
                       "Claudia"),
         last_name = c("Mendes","Stewart",
                       "Abrahamson","Hickman","Harper","Miller","Landy","Jordan",
                       "Berry","Garcia"),
         job_title = c("Professional","Programmer",
                       "Management","Clerical","Developer","Programmer",
                       "Management","Clerical","Developer","Programmer")
)

employee %>%
    unite(name, first_name, last_name, sep = " ")
#>    id              name    job_title
#> 1   1       John Mendes Professional
#> 2   2       Rob Stewart   Programmer
#> 3   3 Rachel Abrahamson   Management
#> 4   4   Christy Hickman     Clerical
#> 5   5    Johnson Harper    Developer
#> 6   6    Candace Miller   Programmer
#> 7   7     Carlson Landy   Management
#> 8   8      Pansy Jordan     Clerical
#> 9   9      Darius Berry    Developer
#> 10 10    Claudia Garcia   Programmer

Created on 2022-06-23 by the reprex package (v2.0.1)

Can you try again to make a reproducible example that actually shows your problem?

1 Like

These are the steps I took:

id <- c(1:10)
name <- c("John Mendes", "Rob Stewart", "Rachel Abrahamson", "Christy Hickman", "Johnson Harper", "Candace Miller", "Carlson Landy", "Pansy Jordan", "Darius Berry", "Claudia Garcia")
job_title <- c("Professional", "Programmer", "Management", "Clerical", "Developer", "Programmer", "Management", "Clerical", "Developer", "Programmer")
employee <- data.frame(id,name,job_title)
employee
id name job_title
1 1 John Mendes Professional
2 2 Rob Stewart Programmer
3 3 Rachel Abrahamson Management
4 4 Christy Hickman Clerical
5 5 Johnson Harper Developer
6 6 Candace Miller Programmer
7 7 Carlson Landy Management
8 8 Pansy Jordan Clerical
9 9 Darius Berry Developer
10 10 Claudia Garcia Programmer

install.packages("dplyr")

separate(employee,name,into=c('first_name','last_name'),sep=' ')
id first_name last_name job_title
1 1 John Mendes Professional
2 2 Rob Stewart Programmer
3 3 Rachel Abrahamson Management
4 4 Christy Hickman Clerical
5 5 Johnson Harper Developer
6 6 Candace Miller Programmer
7 7 Carlson Landy Management
8 8 Pansy Jordan Clerical
9 9 Darius Berry Developer
10 10 Claudia Garcia Programmer

unite(employee,'name',first_name,last_name,sep=' ')
Error in chr_as_locations():
! Can't subset columns that don't exist.
x Column first_name doesn't exist.
Run rlang::last_error() to see where the error occurred.

tidyverse packages do not perform in-place modifications, the dataframe you are showing has a name column, when you separate it into first_name and last_name , you are not saving the changes back to the original data frame so when you try to apply tidyr::unite() on the original data frame, those columns do not exist. If you want changes to persist, you have to explicitly assign them. For example:

employee <- separate(employee,name,into=c('first_name','last_name'),sep=' ')

Thank you andresrcs. Troubleshooting was successful. I appreciate!

employee<- separate(employee,name,into=c('first_name','last_name'),sep=' ')

unite(employee,'name',first_name,last_name,sep=' ')

id name job_title
1 1 John Mendes Professional
2 2 Rob Stewart Programmer
3 3 Rachel Abrahamson Management
4 4 Christy Hickman Clerical
5 5 Johnson Harper Developer
6 6 Candace Miller Programmer
7 7 Carlson Landy Management
8 8 Pansy Jordan Clerical
9 9 Darius Berry Developer
10 10 Claudia Garcia Programmer

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.