1

I think I am missing something important. Every time I am trying to separate a column which has "." delimiter I fail.

Does anyone have an idea why?

library(tidyverse)
library(reprex)

check=data.frame(time=c("1.2","1.3"),light=c("A","B"))
check
#>   time light
#> 1  1.2     A
#> 2  1.3     B

check %>% 
  tidyr::separate(time,c('time2',"node"),sep='.')
#> Warning: Expected 2 pieces. Additional pieces discarded in 2 rows [1, 2].
#>   time2 node light
#> 1                A
#> 2                B

Created on 2022-01-06 by the reprex package (v2.0.1)

For any other delimiter separate works well e.g.,

library(tidyverse)
library(reprex)

check=data.frame(time=c("1_2","1_3"),light=c("A","B"))
check
#>   time light
#> 1  1_2     A
#> 2  1_3     B

check %>% 
  tidyr::separate(time,c('time2',"node"),sep='_')
#>   time2 node light
#> 1     1    2     A
#> 2     1    3     B

Created on 2022-01-06 by the reprex package (v2.0.1)

LDT
  • 2,856
  • 2
  • 15
  • 32

1 Answers1

1

sep is in regex mode according to ?separate

sep - If character, sep is interpreted as a regular expression. The default value is a regular expression that matches any sequence of non-alphanumeric values.

and . is a metacharacter according to ?regex

The metacharacters in extended regular expressions are . \ | ( ) [ { ^ $ * + ?, but note that whether these have a special meaning depends on the context. The period . matches any single character.

Escape (\\) or place it inside square brackets ([.]) to evaluate it literally

library(dplyr)
check %>% 
  tidyr::separate(time,c('time2',"node"),sep='\\.')

-output

   time2 node light
1     1    2     A
2     1    3     B
akrun
  • 874,273
  • 37
  • 540
  • 662