Hello, assume I have an "unstructured" text line from a connection. Unfortunately, it is in string format: R> x [1] "\talpha0\t-0.638\t0.4043\t0.4043\t-2.215\t-0.5765\t-0.137\t501\t2000" How can I extract the data included in this string object "x" in order to get the elements for the parameter vector called "alpha0", i.e. -0.638 0.4043 0.0467 0.4043 -2.215 -0.5765 -0.137 501 Any hints how to handle this would be appreciated. Best regards, Christine -- Christine Adrion, Dipl.-Stat., MPH Ludwig-Maximilians-Universitaet Muenchen IBE - Institut fuer Medizinische Informations- verarbeitung, Biometrie und Epidemiologie Marchioninistr. 15 D- 81377 Muenchen Tel.: +49 (0)89 7095 - 4483 eMail: adrion@ibe.med.uni-muenchen.de web: http://ibe.web.med.uni-muenchen.de -- View this message in context: http://www.nabble.com/How-to-cut-data-elements-included-in-a-text-line-tp18533319p18533319.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Henrique Dallazuanna
2008-Jul-18 18:07 UTC
[R] How to cut data elements included in a text line
Try this: na.omit(as.numeric(unlist(strsplit("\talpha0\t-0.638\t0.4043\t0.4043\t-2.215\t-0.5765\t-0.137\t501\t2000", "\t")))) On Fri, Jul 18, 2008 at 1:44 PM, Christine A. <adrion at ibe.med.uni-muenchen.de> wrote:> > Hello, > > assume I have an "unstructured" text line from a connection. Unfortunately, > it is in string format: > > R> x > [1] "\talpha0\t-0.638\t0.4043\t0.4043\t-2.215\t-0.5765\t-0.137\t501\t2000" > > > How can I extract the data included in this string object "x" in order to > get the elements for the parameter vector called "alpha0", i.e. > -0.638 0.4043 0.0467 0.4043 -2.215 -0.5765 -0.137 501 > > > Any hints how to handle this would be appreciated. > Best regards, > Christine > > > -- > Christine Adrion, Dipl.-Stat., MPH > > Ludwig-Maximilians-Universitaet Muenchen > IBE - Institut fuer Medizinische Informations- > verarbeitung, Biometrie und Epidemiologie > Marchioninistr. 15 > D- 81377 Muenchen > > Tel.: +49 (0)89 7095 - 4483 > eMail: adrion at ibe.med.uni-muenchen.de > web: http://ibe.web.med.uni-muenchen.de > > > > > > > > > -- > View this message in context: http://www.nabble.com/How-to-cut-data-elements-included-in-a-text-line-tp18533319p18533319.html > Sent from the R help mailing list archive at Nabble.com. > > [[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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Jorge Ivan Velez
2008-Jul-18 18:10 UTC
[R] How to cut data elements included in a text line
Dear Christine, Try x="\talpha0\t-0.638\t0.4043\t0.4043\t-2.215\t-0.5765\t-0.137\t501\t2000" res=unlist(strsplit(x,"[\t]")) as.numeric(res[-c(1,2,length(res)-1,length(res))]) HTH, Jorge On Fri, Jul 18, 2008 at 12:44 PM, Christine A. < adrion@ibe.med.uni-muenchen.de> wrote:> > Hello, > > assume I have an "unstructured" text line from a connection. Unfortunately, > it is in string format: > > R> x > [1] "\talpha0\t-0.638\t0.4043\t0.4043\t-2.215\t-0.5765\t-0.137\t501\t2000" > > > How can I extract the data included in this string object "x" in order to > get the elements for the parameter vector called "alpha0", i.e. > -0.638 0.4043 0.0467 0.4043 -2.215 -0.5765 -0.137 501 > > > Any hints how to handle this would be appreciated. > Best regards, > Christine > > > -- > Christine Adrion, Dipl.-Stat., MPH > > Ludwig-Maximilians-Universitaet Muenchen > IBE - Institut fuer Medizinische Informations- > verarbeitung, Biometrie und Epidemiologie > Marchioninistr. 15 > D- 81377 Muenchen > > Tel.: +49 (0)89 7095 - 4483 > eMail: adrion@ibe.med.uni-muenchen.de > web: http://ibe.web.med.uni-muenchen.de > > > > > > > > > -- > View this message in context: > http://www.nabble.com/How-to-cut-data-elements-included-in-a-text-line-tp18533319p18533319.html > Sent from the R help mailing list archive at Nabble.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. >[[alternative HTML version deleted]]
Gabor Grothendieck
2008-Jul-18 18:46 UTC
[R] How to cut data elements included in a text line
strapply in gsubfn finds matches of the indicated regexp, in this case a \t followed by one or more minus, dot or digit, and with backref of -1 it passes the backreference, i.e. portion of the match within (..), to the function in the third arg. See http://gsubfn.googlecode.com library(gsubfn) strapply(x, "\t([-.0-9]+)", as.numeric, backref = -1)[[1]] On Fri, Jul 18, 2008 at 12:44 PM, Christine A. <adrion at ibe.med.uni-muenchen.de> wrote:> > Hello, > > assume I have an "unstructured" text line from a connection. Unfortunately, > it is in string format: > > R> x > [1] "\talpha0\t-0.638\t0.4043\t0.4043\t-2.215\t-0.5765\t-0.137\t501\t2000" > > > How can I extract the data included in this string object "x" in order to > get the elements for the parameter vector called "alpha0", i.e. > -0.638 0.4043 0.0467 0.4043 -2.215 -0.5765 -0.137 501 > > > Any hints how to handle this would be appreciated. > Best regards, > Christine > > > -- > Christine Adrion, Dipl.-Stat., MPH > > Ludwig-Maximilians-Universitaet Muenchen > IBE - Institut fuer Medizinische Informations- > verarbeitung, Biometrie und Epidemiologie > Marchioninistr. 15 > D- 81377 Muenchen > > Tel.: +49 (0)89 7095 - 4483 > eMail: adrion at ibe.med.uni-muenchen.de > web: http://ibe.web.med.uni-muenchen.de > > > > > > > > > -- > View this message in context: http://www.nabble.com/How-to-cut-data-elements-included-in-a-text-line-tp18533319p18533319.html > Sent from the R help mailing list archive at Nabble.com. > > [[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. >