加载R包
library(tidyverse)
str_c函数
#str_c
str_c("today","is","sunday",sep=" ")
str_c(c("today","is","sunday"),collapse=" ")
str_c("x_",1:12,"_y")
image.png
#str_sub:字符串取子集
x<-c("Fast Furious",
"Captain America:The First Avenger",
"X-Men Origins: Wolverine",
"Iron Man")
str_sub(x,1,3)
image.png
将向量x里的字符数据首字母变为小写字母
str_sub(x,1,1)<-str_to_lower(str_sub(x,1,1))
image.png
向量提取首字母以t开头的数据
image.png
str_detect和str_subset函数
#str_detect,str_subset:模式匹配
fruit[str_detect(fruit,"^t")]
str_subset(fruit,"^t")
image.png
v_detail<-read_csv("vip_consumption_detail.csv",
col_names=T)
v_detail.png
kh_name<-v_detail %>%
group_by(kh) %>% #将kh分组
summarise(commodity_name=str_c(unique(spmc),collapse = ","))
kh_name.png
str_extract和str_extract_all函数
#str_extract,str_extract_all:提取匹配内容
yaf<-str_subset(kh_name$commodity_name,"^雅芳")
str_extract(yaf,"^雅芳")
str_extract_all(yaf,"^雅芳")
yaf.png
str_extract.png
str_extract_all.png
#str_replace,str_replace_all:替换匹配内容
str_replace(yaf[1:10],"^雅芳","化妆品8888")
image.png
网友评论