str_replace() 使用来自 SPDF、stringr 包的向量转义 R 中的问号
str_replace() escaping question marks in R with a vector from a SPDF, stringr package
我很不好意思问这个问题,但我已经尝试使用 gsub
,现在是 stringr
包。
我无法完成这些字符串替换:
str_replace(mexispdf@data$ADM1NAME,pattern = "M\?xico",replacement = "México")
str_replace(mexispdf@data$ADM1NAME,pattern = "Nuevo Le\?n" ,replacement = "Nuevo León")
str_replace(mexispdf@data$ADM1NAME,pattern = "San Luis Potos\?",replacement = "San Luis Potosí")
str_replace(mexispdf@data$ADM1NAME,pattern = "Quer\?taro de Arteaga",replacement = "Querétaro de Arteaga")
我认为问题是 ADM1NAME
是一个因素而不是一个字符,所以我改变了它:
mexispdf@data$ADM1NAME<-as.character(mexispdf@data$ADM1NAME)
但是还是不行:
只需下载墨西哥 shapefile。
with gsub
使用 fixed=TRUE
参数对我有用:
a = "M\?xico"
gsub(pattern = "\?",replacement = "é",a, fixed = TRUE)
# "México"
有了 stringr
你可以做到
> str_replace_all("M\?xico", pattern = c("[^M?xico]"="é", "[?]"=""))
[1] "México"
library(stringr)
str_replace_all("Is Nuevo Leon in Mexico?",
c("M.xico" = "México", "Nuevo Le.n" = "Nuevo León"))
# [1] "Is Nuevo León in México?"
我很不好意思问这个问题,但我已经尝试使用 gsub
,现在是 stringr
包。
我无法完成这些字符串替换:
str_replace(mexispdf@data$ADM1NAME,pattern = "M\?xico",replacement = "México")
str_replace(mexispdf@data$ADM1NAME,pattern = "Nuevo Le\?n" ,replacement = "Nuevo León")
str_replace(mexispdf@data$ADM1NAME,pattern = "San Luis Potos\?",replacement = "San Luis Potosí")
str_replace(mexispdf@data$ADM1NAME,pattern = "Quer\?taro de Arteaga",replacement = "Querétaro de Arteaga")
我认为问题是 ADM1NAME
是一个因素而不是一个字符,所以我改变了它:
mexispdf@data$ADM1NAME<-as.character(mexispdf@data$ADM1NAME)
但是还是不行:
只需下载墨西哥 shapefile。
with gsub
使用 fixed=TRUE
参数对我有用:
a = "M\?xico"
gsub(pattern = "\?",replacement = "é",a, fixed = TRUE)
# "México"
有了 stringr
你可以做到
> str_replace_all("M\?xico", pattern = c("[^M?xico]"="é", "[?]"=""))
[1] "México"
library(stringr)
str_replace_all("Is Nuevo Leon in Mexico?",
c("M.xico" = "México", "Nuevo Le.n" = "Nuevo León"))
# [1] "Is Nuevo León in México?"