Error in `chr_as_locations()`:

I created a df called 'employee', and separated the name col, into 'first_name ' & 'last_name'. I am now trying to unite the 'first_name' col with the 'last_name' col into a new col called 'full_name', but getting an error indicating that the 'first_name' col does not exist when it clearly does.

library("tidyverse")

id <- c(1:2)

name <- c("John Mendes", "Rob Stewart")

job_title <- c("Professional", "Programmer")

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

print(employee)
#>   id        name    job_title
#> 1  1 John Mendes Professional
#> 2  2 Rob Stewart   Programmer

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

unite(employee,'full_name',first_name,last_name,sep=' ')
#> Error in `chr_as_locations()`:
#> ! Can't subset columns that don't exist.
#> ✖ Column `first_name` doesn't exist.

Created on 2022-10-31 with reprex v2.0.2

You are not storing the result of your separate and unite functions.

library("tidyverse")

id <- c(1:2)

name <- c("John Mendes", "Rob Stewart")

job_title <- c("Professional", "Programmer")

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

print(employee)
#>   id        name    job_title
#> 1  1 John Mendes Professional
#> 2  2 Rob Stewart   Programmer
#Not storing the results
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


unite(employee,'full_name',first_name,last_name,sep=' ',remove = FALSE)
#> Error in `chr_as_locations()`:
#> ! Can't subset columns that don't exist.
#> ✖ Column `first_name` doesn't exist.

#store the results
employee <- separate(employee,name,into = c('first_name','last_name'), sep = ' ')
employee <- unite(employee,'full_name',first_name,last_name,sep=' ',remove = FALSE)
employee
#>   id   full_name first_name last_name    job_title
#> 1  1 John Mendes       John    Mendes Professional
#> 2  2 Rob Stewart        Rob   Stewart   Programmer

Created on 2022-10-31 with reprex v2.0.2

1 Like

Thanks, works when I store the results.

This topic was automatically closed 7 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.