1

I wonder if there is some secret argument that would allow to apply separate from the end of the line? Some magic_argument?

The desired output would be as follows:

library(dplyr)
df <- data.frame(x = c(NA, "a.b.b", "a.b.d", "b.c"))
df %>% separate(x, c("A", "B"), magic_argument = TRUE)
#>      A    B
#> 1 <NA> <NA>
#> 2    a.b    b
#> 3    a.b    d
#> 4    b    c
JelenaČuklina
  • 3,574
  • 2
  • 22
  • 35
  • [From this answer](https://stackoverflow.com/a/41028910/5635580), you can try `transform(df, x = sub("(.*)\\..*", "\\1", x), B = sub(".*\\.", "", x))` – Sotos Feb 02 '18 at 13:17

1 Answers1

5

Try:

df %>% separate(x, c("A", "B"), sep="\\.(?=[^\\.]+$)")
#     A    B
#1 <NA> <NA>
#2  a.b    b
#3  a.b    d
#4    b    c
nicola
  • 24,005
  • 3
  • 35
  • 56