[R] How to get a substring from a string

Yuan, Rebecca rebecca.yuan at bankofamerica.com
Wed Jan 15 16:46:58 CET 2014


Hello A.K.,

This gives more flexibility to substract the string, thanks very much!

Cheers,

Rebecca


-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com] 
Sent: Wednesday, January 15, 2014 10:43 AM
To: R help
Cc: Yuan, Rebecca
Subject: Re: [R] How to get a substring from a string

Hi,
Try:

  sub("~.*","",string)
#[1] "yab"

#or
 as.character(as.formula(string)[[2]])
#[1] "yab"

A.K.


On Wednesday, January 15, 2014 9:40 AM, "Yuan, Rebecca" <rebecca.yuan at bankofamerica.com> wrote:
Hello all,

I would like to get a substring (first few characters till ~) from a string

I can think of way of doing so by

> string<-'yab~a+b+c'
> x<-substring(string,1,3)
> x
[1] "yab"

But if I do not know the length of the first word before ~ sign, how can I get it out from the whole string?

Thanks,

Rebecca

----------------------------------------------------------------------
This message, and any attachments, is for the intended r...{{dropped:15}}




More information about the R-help mailing list