RE: [R] removing characters from a string

From: John Fox <jfox_at_mcmaster.ca>
Date: Tue 12 Apr 2005 - 23:14:17 EST


Dear Vivek,

Actually, I think R has reasonably good facilities for manipulating strings. See ?gsub etc.; for example:

gsub("[^0-9]", "", "XKa0&*1jk2")
[1] "012"

I hope this helps,
 John



John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Vivek Rao
> Sent: Tuesday, April 12, 2005 7:55 AM
> To: r-help@stat.math.ethz.ch
> Subject: [R] removing characters from a string
>
> Is there a simple way in R to remove all characters from a
> string other than those in a specified set? For example, I
> want to keep only the digits 0-9 in a string.
>
> In general, I have found the string handling abilities of R a
> bit limited. (Of course it's great for stats in general). Is
> there a good reference on this? Or should R programmers dump
> their output to a text file and use something like Perl or
> Python for sophisticated text processing?
>
> I am familiar with the basic functions such as nchar,
> substring, as.integer, print, cat, sprintf etc.
>



R-help@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 Received on Tue Apr 12 23:41:02 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:07 EST