r - Separate column into three columns with grouping


Keywords:r 


Question: 


2 Answers: 

Use extra argument:

# dummy data
df1 <- data.frame(x = c(
  "some name1",
  "justOneName",
  "some three name",
  "Abdullaeva Mehseti Nuraddin Kyzy"))

library(tidyr)
library(dplyr)

df1 %>% 
  separate(x, c("a1", "a2", "a3"), extra = "merge")
#            a1      a2            a3
# 1        some   name1          <NA>
# 2 justOneName    <NA>          <NA>
# 3        some   three          name
# 4  Abdullaeva Mehseti Nuraddin Kyzy
# Warning message:
#   Too few values at 2 locations: 1, 2 

From manual:

extra

If sep is a character vector, this controls what happens when there are too many pieces. There are three valid options:
- "warn" (the default): emit a warning and drop extra values.
- "drop": drop any extra values without a warning.
- "merge": only splits at most length(into) times

 

Since for this dataset you said that you only have name1, name2, last name, then you can also use str_split_fixed from stringr, i.e.

setNames(data.frame(stringr::str_split_fixed(df1$x, ' ', 3)), paste0('a', 1:3))

Which gives,

        a1      a2            a3
1        some   name1              
2 justOneName                      
3        some   three          name
4  Abdullaeva Mehseti Nuraddin Kyzy

Note that you can fill the empty slots with NA as per usual