[R] remove substring from each string vector component
Marc Schwartz
marc_schwartz at me.com
Tue Mar 16 15:59:22 CET 2010
On Mar 16, 2010, at 9:50 AM, arnaud chozo wrote:
> Hi all,
>
> I have a string vector like that: x=c("1\t\t", "2", "3\t\t\t")
> I need to remove all the occurrences of "\t", in order to obtain: x = "1"
> "2" "3"
>
> I'm trying to use the function substring2, and it works for each component,
> for example:
> substring2(x[1], "\t") =""
> gives x = "1" "2" " 3\t\t\t"
>
> I'd like to apply this function to each component and I tried the following:
>
> myfun = function(x, i) {
> substring2(x[i], "\t") = ""
> }
>
> lapply(x, myfun)
>
> which gives x="" "" ""
>
> I know that I'm wrong somewhere using lapply, but I can't fix it.
>
> Thanks in advance,
> Arnaud Chozo
This is a place to use regular expressions with gsub():
x <- c("1\t\t", "2", "3\t\t\t")
> gsub("\\t", "", x)
[1] "1" "2" "3"
See ?regex and ?gsub
Note that you have to double the backslashes in the regex.
HTH,
Marc Schwartz
More information about the R-help
mailing list