[R] Re: how to separate a string

Rich FitzJohn rich.fitzjohn at gmail.com
Wed Apr 13 08:38:18 CEST 2005


To get the first character from a string, use substr(s, 1, 1)

To split strings at a period, use strsplit(s, "\\.") (the period must
be quoted, as "." matches anything in a regular expression).

To get the index for "." in a string, see "?regexpr, but you will not
need to do that if you use strsplit().

Cheers,
Rich

On 4/13/05, Cuichang Zhao <cuiczhao at yahoo.com> wrote:
> hello, 
> i wonder how is string represent in R. if i have a string s= "hello", how
> can i refer to first character in the string s? 
> also if i have s1 = "hello.1", s2 = "ok.1",  how can i separate the s1 into
> "hello" "1" and s2 into "ok" and "1"? I have tried to use the substring
> function, but i don't where i can get the index for "." in the string?
>  
> Thank you so much
>  
> C-Ming
> April 12, 2005
> 
> 		
> ---------------------------------
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> 

-- 
Rich FitzJohn
rich.fitzjohn <at> gmail.com   |    http://homepages.paradise.net.nz/richa183
                      You are in a maze of twisty little functions, all alike




More information about the R-help mailing list