R 删除双空格
我很难解决这个问题。我想创建一个包含每个标点符号(;-,)计数的绘图。 我有这个密码R 删除双空格,r,regex,dataframe,R,Regex,Dataframe,我很难解决这个问题。我想创建一个包含每个标点符号(;-,)计数的绘图。 我有这个密码 wekto<-c("Robot, robot; Nose, nose; Robot, robot; Toes, toes; Robot, robot - touch your nose. Robot, robot - touch your toes.") stops <- function(text){ wekto%>% str_remove_all(&
wekto<-c("Robot, robot; Nose, nose; Robot, robot; Toes, toes; Robot, robot - touch your nose. Robot, robot - touch your toes.")
stops <- function(text){
wekto%>%
str_remove_all("[A-z]") %>%
str_split(pattern = " ") %>%
table() %>%
as.data.frame %>%
setNames(c("Punctuation","Count"))%>%
ggplot(aes(x=Punctuation,y=Count)) +
geom_col() +
coord_flip()+
theme_classic()+
theme(axis.text.y = element_text(size = 17))
}
wekto%
str_split(pattern=“”)%>%
表()%>%
as.data.frame%>%
集合名(c(“标点符号”,“计数”))%>%
ggplot(aes(x=标点符号,y=计数))+
geom_col()+
coord_flip()+
主题(经典)+
主题(axis.text.y=元素\文本(大小=17))
}
但我希望它在显示时不带空格,因为有两个空格,所以会将空格添加到我的数据框中。
我怎样才能移除它们?
我用
str\u remove
和str\u replace
尝试了它,但它不起作用您可以通过在集合名之后将以下代码插入管道链来解决此问题:
subset(Punctuation != "")
或者考虑为正则表达式添加空间:
str_remove_all("[A-z\\s]") %>%
str_split("")
但是,我也要注意不要使用大小写混合正则表达式,如a-z
。您最终将包含比您想象的更多的字符:
您可能需要考虑<代码> -AZ-Z 或字符类,如<代码> \\W或<代码> [CITTT: ] /> >:< /P>
gsub("[^[:punct:]]+", "", wekto) %>%
str_split("") %>%
table(dnn = "Punctuation") %>%
as.data.frame(responseName = "Count") %>%
ggplot(aes(x=Punctuation,y=Count)) +
geom_col() +
coord_flip()+
theme_classic()+
theme(axis.text.y = element_text(size = 17))
谢谢你的帮助!谢谢你的建议:)