• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

R语言字符串函数

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

字符串长度:

nchar("hello world")

#字符串连接:
paste() #paste(..., sep = " ", collapse = NULL)

#字符串分割:
strsplit() #strsplit(x, split, extended = TRUE, fixed = FALSE, perl = FALSE)

aa<-unlist(strsplit(x, 'char'))

aa[1] # first string



#计算字符串的字符数:
nchar()

#字符串截取:
substr(xstartstop)
substring(textfirstlast = 1000000)
substr(xstartstop) <- value
substring(textfirstlast = 1000000) <- value

 substr("abcdef", 2, 4)
[1] "bcd"

> x <- "1234567890"
> substr(x, 3, 3)
[1] "3"
> 
> substr(x, 5, 7)
[1] "567"
> 
> substr(x, 4, 4) <- "A"
> x
[1] "123A567890"
> 
> substr(x, 2, 4) <- "TTF"
> x
[1] "1TTF567890"
> 
> substr(x, 9, 12) <- "ABCD"
> x
[1] "1TTF5678AB"
> 
> substring(x, 5)
[1] "5678AB"
> 
> substring(x, 5) <- "..."
> x
[1] "1TTF...8AB"




#字符串替换及大小写转换:
chartr(oldnewx)
tolower(x)
toupper(x)
casefold(xupper = FALSE)

x=gsub(old_strin, new_string, x) # replace string 字符串替换

## 包含子字符串: chars中是否包含value

grepl(value, chars) # chars contains value? (TRUE, FALSE)

 

#字符串比较:

if(as.character(x)==as.character(y))


判断字符串是否包含某个子串
> chars <- "test"
> value <- "es"
> grepl(value, chars)

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言学习-线图一步法发布时间:2022-07-18
下一篇:
R语言学习——欧拉计划(1)Multiplesof3and5发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap