R语言-连接、提取字符、分割字符、替换字符函数学习

连接函数paste,用于连接字符
#paste function
#join together two or more strings into one string
paste("I","love","R language.")
paste("I","love","English.",sep="-")
paste("I","love","English.",sep="")
paste("1+1=",2)
t1=c("Jim","Sunny","Evan")
paste(t1,"love","Maths")

#substr function
#If you want to extract a portion of a string according to position.
#you can use the substr(string,start,end),it can  extract the substring that begins at start and ends at end.
str="ABCDEFG"
substr(str,1,4)#return "ABCD"
substr(str,3,6)#return "CDEF"
str1=c("Test","Yes","Thanks")
substr(str1,1,3) # return "Tes" "Yes" "Tha"
cities=c("New York, NY", "Los Angeles, CA", "Peoria, IL")
substr(cities, nchar(cities)-1, nchar(cities))

#strsplit function
#If you want to split a string into substrings,you can use it.
#strsplit(string, delimiter)
str="home/mike/data/trials.csv"
strsplit(str,"/") #return each substring
str1=c("home/mike/data/trials.csv","home/mike/data/errors.csv","home/mike/corr/reject.doc")
strsplit(str1,"/")

#Replacing Substrings
#Use sub to replace the first instance of a substring
#sub(old,new,string)
#use gsub to replace all instances of a substring
#gsub(old,new,string)
str="I hate USA.I love dancing."
sub("I","You",str)
gsub("I","You",str)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值