Hi all I am trying t extract a variable from a column ASk/20005-01-45/90 Alldatk/25-17-4567/990 I want to assign a variable to the numbers coming the first"-" x=01 for the first and x=17 for teh second I tried using gsub but did not work x=gsub("-") any help? [[alternative HTML version deleted]]
Dear Val, Your question isn't entirely clear (to me), but this is what I think you want to do: ------------------ snip ----------------> strings <- c("ASk/20005-01-45/90", "Alldatk/25-17-4567/990") > location <- regexpr("-[0-9]*", strings) > x[1] "01" "17"> x <- substring(strings, location + 1, location + attr(location, "match.length") - 1) > as.numeric(x)[1] 1 17 ------------------ snip ---------------- I hope this helps, John ----------------------------- John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 web: socserv.mcmaster.ca/jfox ________________________________________ From: R-help [r-help-bounces at r-project.org] on behalf of Val [valkremk at gmail.com] Sent: April 8, 2016 10:21 PM To: r-help at R-project.org (r-help at r-project.org) Subject: [R] assign Hi all I am trying t extract a variable from a column ASk/20005-01-45/90 Alldatk/25-17-4567/990 I want to assign a variable to the numbers coming the first"-" x=01 for the first and x=17 for teh second I tried using gsub but did not work x=gsub("-") any help? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
You are not using that function as it was designed to be used. You should read the help for gsub... ?gsub And if you don't know what the term "regular expression pattern" that is mentioned there means then you will probably need to study one of the many fine tutorials that are available on the Web on that topic to understand how gsub( "^.*-([^-]+)-.*$", "\\1", c( "junk-01-more", "stuff-17-" ) ) matches the entire string (^ to $) while capturing (parentheses) one or more non-dash characters ([^-]+) between the first dash and the second dash and substituting that "first capture" in place of the entire string. Note that the regular expression only needs one \ before the 1 but the R parser requires you to "escape" that with another \ to get that one \ into memory. And while you are studying, be sure to read and heed the R Mailing Lists Posting Guide mentioned in every post on this list, because you used HTML format which tends to mess up R code examples. -- Sent from my phone. Please excuse my brevity. On April 8, 2016 7:21:06 PM PDT, Val <valkremk at gmail.com> wrote:>Hi all > I am trying t extract a variable from a column > > ASk/20005-01-45/90 > > Alldatk/25-17-4567/990 > >I want to assign a variable to the numbers coming the first"-" > >x=01 for the first and >x=17 for teh second > >I tried using gsub but did not work > >x=gsub("-") > >any help? > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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]]
Hi, I couldn't resist these two suggestions: strings <- c("ASk/20005-01-45/90", "Alldatk/25-17-4567/990") x <- as.numeric(gsub("^[^-]*-|-.*$","",strings)) or x <- as.numeric(sub("^[^-]*-([0-9]+)-.*$","\\1",strings)) Best, Georges --------------------- Georges Monette, York University, Toronto On 08/04/2016 10:53 PM, Fox, John wrote:> Dear Val, > > Your question isn't entirely clear (to me), but this is what I think you want to do: > > ------------------ snip ---------------- > >> strings <- c("ASk/20005-01-45/90", "Alldatk/25-17-4567/990") >> location <- regexpr("-[0-9]*", strings) >> x > [1] "01" "17" >> x <- substring(strings, location + 1, location + attr(location, "match.length") - 1) >> as.numeric(x) > [1] 1 17 > > ------------------ snip ---------------- > > I hope this helps, > John > > ----------------------------- > John Fox, Professor > McMaster University > Hamilton, Ontario > Canada L8S 4M4 > web: socserv.mcmaster.ca/jfox > > > ________________________________________ > From: R-help [r-help-bounces at r-project.org] on behalf of Val [valkremk at gmail.com] > Sent: April 8, 2016 10:21 PM > To: r-help at R-project.org (r-help at r-project.org) > Subject: [R] assign > > Hi all > I am trying t extract a variable from a column > > ASk/20005-01-45/90 > > Alldatk/25-17-4567/990 > > I want to assign a variable to the numbers coming the first"-" > > x=01 for the first and > x=17 for teh second > > I tried using gsub but did not work > > x=gsub("-") > > any help? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.