如何分隔“.”用 tidyR 分隔的列

How to separate "." delimited columns with tidyR

我想我遗漏了一些重要的东西。每次我试图分隔一个带有“。”的列时。分隔符我失败了。

有人知道为什么吗?

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

reprex package (v2.0.1)

创建于 2022-01-06

对于任何其他分隔符分隔效果很好,例如,

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

reprex package (v2.0.1)

创建于 2022-01-06

sep 根据 ?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.

并且 . 是根据 ?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.

转义 (\) 或将其放在方括号内 ([.]) 以按字面意思对其求值

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

-输出

   time2 node light
1     1    2     A
2     1    3     B