I have a data frame with a dot-separated character column:
> set.seed(310366)
> tst = data.frame(x=1:10,y=paste(sample(c(\"FOO\",\"BAR\",\"BAZ\"),10,TR
This answer applies here as well; the following approach is both tidyverse-idiomatic and more performant than separate() (as of 2020):
set.seed(310366)
tst = data.frame(x=1:10,y=paste(sample(c("FOO","BAR","BAZ"),10,TRUE),".",sample(c("foo","bar","baz"),10,TRUE),sep=""))
library(dplyr)
library(purrr)
tst %>%
mutate(tmp_chunks = stringr::str_split(y, fixed("."), n = 2)) %>%
mutate(y1 = map_chr(tmp_chunks, 1),
y2 = map_chr(tmp_chunks, 2)) %>%
select(-tmp_chunks)
... Or if you don't want y anymore after splitting it, you can change the last line to
select(-tmp_chunks, -y)
You can use separate() from tidyr in combination with dplyr:
tst %>% separate(y, c("y1", "y2"), sep = "\\.", remove=FALSE)
x y y1 y2
1 1 BAR.baz BAR baz
2 2 FOO.foo FOO foo
3 3 BAZ.baz BAZ baz
4 4 BAZ.foo BAZ foo
5 5 BAZ.bar BAZ bar
6 6 FOO.baz FOO baz
7 7 BAR.bar BAR bar
8 8 BAZ.baz BAZ baz
9 9 FOO.bar FOO bar
10 10 BAR.foo BAR foo
Setting remove=TRUE will remove column y