Vijayan Padmanabhan
2011-Jun-18 08:36 UTC
[R] how to subtract one string from another in R
Dear R Group Here is what i am trying to do.. but couldnt figure out how.. string<-"ABC DEFG HIJKLM NOPQ RSTUV WXY" string1<-substr(string,1,4) I want to create an R object string 2 ( following the logic shown).. R does not allow string subtraction.. any suggestions how to achieve this? string2<-string-string1 (it should now hold "DEFG HIJKLM NOPQ RSTUV WXY" I want to loop this till i reach the end of the original string.. stringn<-"WXY" Any help would be appreciated. Regards Vijayan Padmanabhan [[alternative HTML version deleted]]
Hello Vijayan, Depending on your end goal the following could possibly help substr(string,nchar(string1)+1, nchar(string)) or strsplit(string," ") HTH, Santosh On Sat, Jun 18, 2011 at 2:06 PM, Vijayan Padmanabhan <padmanabhan.vijayan at gmail.com> wrote:> Dear R Group > Here is what i am trying to do.. but couldnt figure out how.. > > string<-"ABC DEFG HIJKLM NOPQ RSTUV WXY" > string1<-substr(string,1,4) > > > I want to create an R object string 2 ( following the logic shown).. R does > not allow string subtraction.. any suggestions how to achieve this? > > > string2<-string-string1 (it should now hold "DEFG HIJKLM NOPQ RSTUV WXY" > > I want to loop this till i reach the end of the original string.. > > stringn<-"WXY" > > Any help would be appreciated. > > > > Regards > Vijayan Padmanabhan > > ? ? ? ?[[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. >
Is this what you're looking for? string <- "ABC DEFG HIJKLM NOPQ RSTUV WXY" unlist(strsplit(string, " ")) [1] "ABC" "DEFG" "HIJKLM" "NOPQ" "RSTUV" "WXY" HTH, Dennis On Sat, Jun 18, 2011 at 1:36 AM, Vijayan Padmanabhan <padmanabhan.vijayan at gmail.com> wrote:> Dear R Group > Here is what i am trying to do.. but couldnt figure out how.. > > string<-"ABC DEFG HIJKLM NOPQ RSTUV WXY" > string1<-substr(string,1,4) > > > I want to create an R object string 2 ( following the logic shown).. R does > not allow string subtraction.. any suggestions how to achieve this? > > > string2<-string-string1 (it should now hold "DEFG HIJKLM NOPQ RSTUV WXY" > > I want to loop this till i reach the end of the original string.. > > stringn<-"WXY" > > Any help would be appreciated. > > > > Regards > Vijayan Padmanabhan > > ? ? ? ?[[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. >
Hi: I think I misread your intentions the first time. Is this more in line with what you want? f <- function(s) { ssp <- unlist(strsplit(string, "")) n <- length(ssp) splits <- grep(' ', ssp) sapply(splits, function(x) paste(ssp[-(1:x)], collapse = "")) }> f(string)[1] "DEFG HIJKLM NOPQ RSTUV WXY" "HIJKLM NOPQ RSTUV WXY" [3] "NOPQ RSTUV WXY" "RSTUV WXY" [5] "WXY" The function returns a vector of substrings rather than individual substrings assigned to separate objects. HTH, Dennis On Sat, Jun 18, 2011 at 1:36 AM, Vijayan Padmanabhan <padmanabhan.vijayan at gmail.com> wrote:> Dear R Group > Here is what i am trying to do.. but couldnt figure out how.. > > string<-"ABC DEFG HIJKLM NOPQ RSTUV WXY" > string1<-substr(string,1,4) > > > I want to create an R object string 2 ( following the logic shown).. R does > not allow string subtraction.. any suggestions how to achieve this? > > > string2<-string-string1 (it should now hold "DEFG HIJKLM NOPQ RSTUV WXY" > > I want to loop this till i reach the end of the original string.. > > stringn<-"WXY" > > Any help would be appreciated. > > > > Regards > Vijayan Padmanabhan > > ? ? ? ?[[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. >