Good morning R list, My apologies if this has *already* answered elsewhere, but I have not found the answer that I am looking for.
I have a character string, i.e. form<-c('~ A + B + C + C / D + E + E / F + G + H + I + J + K + L * M') Now, my aim is to find the position of all those instances of '*' and to remove said '*'. However, I would also like to remove the preceding variable name before the '*', the math operator preceding this, and also the variable name after the '*'. So, here I would like to remove '+L*M' So, far I have come up with the following code: parts<-strsplit(form,' ') index<-which(unlist(parts)=="*") for (i in 1:length(index)){ parts[[1]][index[i]]<-list(NULL) parts[[1]][index[i]+1]<-list(NULL) parts[[1]][index[i]-1]<-list(NULL) parts[[1]][index[i]-2]<-list(NULL) } new.form<-unlist(parts) form<-new.form[0] for (i in 1: length(new.form)){ form<-paste(form,new.form[i], sep="") } However, as you can see, I have had to use strsplit in, what I consider a rather clumsy manner, as the character string (form) has to be in a certain format. All variables and maths operators require a space between them in order for strsplit to work in the manner I require. I would very much like to accomplish what the above code already does, but without the need for the initial character string having the need for the aforementioned spaces. If the list can offer help, I would be most appreciative. Yours Mike Griffiths -- *Michael Griffiths, Ph.D *Statistician *Upstream Systems* 8th Floor Portland House Bressenden Place SW1E 5BH <http://www.google.com/url?q=http%3A%2F%2Fwww.upstreamsystems.com%2F&sa=D&sntz=1&usg=AFrqEzfKYfaAalqvahwrpywpJDL9DxUmWw> Tel +44 (0) 20 7869 5147 Fax +44 207 290 1321 Mob +44 789 4944 145 www.upstreamsystems.com<http://www.google.com/url?q=http%3A%2F%2Fwww.upstreamsystems.com%2F&sa=D&sntz=1&usg=AFrqEzfKYfaAalqvahwrpywpJDL9DxUmWw> *griffi...@upstreamsystems.com <einst...@upstreamsystems.com>* <http://www.upstreamsystems.com/> [[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.