[R] String Handling() for Split a word by a letter

Jim Lemon jim at bitwrit.com.au
Tue Aug 28 12:44:23 CEST 2012


On 08/27/2012 07:29 PM, Rantony wrote:
> Hi,
>
> here im unable to run a string handle function called unpaste().
>
> for eg:- a<- "12345_mydata"
> Actually my requirement what is i need to get , only 12345.  Means that , i
> need the all letter as a word which is before of  first  " _ " - symbol of
> "a". i tried to do with unpaste, it says function not found.  After that i
> tried with "strsplit() ". it ran, but again i need to write another method
> to get again after spliting.
>
> Any other good method is there for this requirement ?
>
Hi Antony,
You probably want:

strsplit(a,"_")[[1]][1]

which can be used for more than one input string

a<-paste(sample(12345:13456,10),"mydata",sep="_")
sapply(strsplit(a,"_"),"[",1)

Jim




More information about the R-help mailing list