gsub() in R is not replacing '.' (dot)

前端 未结 1 581
你的背包
你的背包 2020-11-28 09:09

I want to replace dots in \"2014.06.09\" to \"2014-06-09\". I am using gsub() function for it. If

x <-  \"2014.06.09\"         


        
相关标签:
1条回答
  • 2020-11-28 09:36

    You may need to escape the . which is a special character that means "any character" (from @Mr Flick's comment)

     gsub('\\.', '-', x)
     #[1] "2014-06-09"
    

    Or

    gsub('[.]', '-', x)
    #[1] "2014-06-09"
    

    Or as @Moix mentioned in the comments, we can also use fixed=TRUE instead of escaping the characters.

     gsub(".", "-", x, fixed = TRUE)
    
    0 讨论(0)
提交回复
热议问题