Why does the separate function not create new values that I then can use?

93 views Asked by At

I have used the separate function to split the name column into first_name and last_name, when I run the func:

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

and it works when the table is produced. They become two different values in my console. But then when I try to reunite them, they say that the first_name and last_name column does not exists even when i have tried them in quotes, single quotes, or just by themselves. Is there a way to get the code for the separated function in order for the values to be applied?

Thank you!!

But then when I try to reunite them, they say that the first_name and last_name column does not exists even when i have tried them in quotes, single quotes, or just by themselves. Is there a way to get the code for the separated function in order for the values to be applied?

1

There are 1 answers

3
Ruam Pimentel On

Your code works. See below the full code doing what you are describing:

# Library
library(dplyr)
library(tidyr)

# Fake data - Input
employee <- data.frame(name = c("John Smith", "Mary White", "Aw Slight", "Kile King"))
employee
#>         name
#> 1 John Smith
#> 2 Mary White
#> 3  Aw Slight
#> 4  Kile King

# Separate working
employee2 <- separate(employee, name, into=c('first_name', 'last_name'), sep=' ')
employee2
#>   first_name last_name
#> 1       John     Smith
#> 2       Mary     White
#> 3         Aw    Slight
#> 4       Kile      King

# Unite also working
employee2 %>% unite( "name", first_name:last_name, sep = " ")
#>         name
#> 1 John Smith
#> 2 Mary White
#> 3  Aw Slight
#> 4  Kile King

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