0

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    Did you assign your `separate(...)` output to on object you used later or did you try to modify `employee` without the `separate`-function assigned to it? – Martin Gal Dec 10 '22 at 20:33

1 Answers1

1

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

Ruam Pimentel
  • 1,288
  • 4
  • 16
  • WOW!! Thank you so much!! That makes so much sense using the pipe function, i was trying to create a new data frame, but of course that did not work!! WOW! Thank you so so much – Brookep44 Dec 10 '22 at 20:51
  • note that `employee2 %>% unite( "name", first_name:last_name, sep = " ")` is the same as `unite(employee2, "name", first_name:last_name, sep = " ")` – Ruam Pimentel Dec 10 '22 at 21:04
  • to create a new dataset you just put the result inside a new object. For example: `employee3 <- employee2 %>% unite( "name", first_name:last_name, sep = " ")` here I'm creating new dataset called `employee3` – Ruam Pimentel Dec 10 '22 at 21:05