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 [[alternative HTML version deleted]]
Hi! It's just this easy: x=gsub("\t","",x) For more complex things, it's worth learning some regular expressions syntax. Miguel On Tue, Mar 16, 2010 at 2:50 PM, arnaud chozo <arnaud.chozo@gmail.com>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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Marc Schwartz
2010-Mar-16 14:59 UTC
[R] remove substring from each string vector component
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 ChozoThis 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
Eik Vettorazzi
2010-Mar-16 15:00 UTC
[R] remove substring from each string vector component
Hi Arnaud, how about this: x=c("1\t\t", "2", "3\t\t\t") gsub("\t","",x) hth. arnaud chozo schrieb:> 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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790