Fredrik Karlsson
2009-Jun-26 11:21 UTC
[R] How do I get just the two last tokens of each string in a vector?
Dear list, Sorry for asking this very silly question on the list, but I seem to have made my life complicated by going into string manipulation in vectors. What I need is to get the last part of a sting (the two last tokens, separated by a space), and of course, this should be done for all strings in a vector, creating a new vector of exual size. So, a <- c(" %L H*L L*H H%", "%L H* H%", "%L L*H %", "%L L*H %" ) should be made into a vector c(" L*H H%", "H* H%", "L*H %", "L*H %" ) I have tried strsplit, but it seems to produce a structure I cannot get to work in this context. Any ideas on how to solve this? Thankful for all the help I can get. /Fredrik -- "Life is like a trumpet - if you don't put anything into it, you don't get anything out of it."
jim holtman
2009-Jun-26 12:00 UTC
[R] How do I get just the two last tokens of each string in a vector?
This should do it> a <- c(" %L H*L L*H H%", "%L H* H%", "%L L*H %", "%L L*H %" ) > # split the strings > x <- strsplit(a, ' ') > # get last two tokens > sapply(x, function(.tokens) paste(tail(.tokens, 2), collapse=' '))[1] "L*H H%" "H* H%" "L*H %" "L*H %"> >On Fri, Jun 26, 2009 at 7:21 AM, Fredrik Karlsson <dargosch@gmail.com>wrote:> Dear list, > > Sorry for asking this very silly question on the list, but I seem to > have made my life complicated by going into string manipulation in > vectors. > What I need is to get the last part of a sting (the two last tokens, > separated by a space), and of course, this should be done for all > strings in a vector, creating a new vector of exual size. > > So, > > a <- c(" %L H*L L*H H%", "%L H* H%", "%L L*H %", "%L L*H %" ) > > should be made into a vector > > c(" L*H H%", "H* H%", "L*H %", "L*H %" ) > > I have tried strsplit, but it seems to produce a structure I cannot > get to work in this context. Any ideas on how to solve this? > > Thankful for all the help I can get. > > /Fredrik > > > -- > "Life is like a trumpet - if you don't put anything into it, you don't > get anything out of it." > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
John Kane
2009-Jun-26 13:47 UTC
[R] How do I get just the two last tokens of each string in a vector?
I have the feeling that this is a very clumsy way to do it but I think it does what you want. ================================================================a <- c(" %L H*L L*H H%", "%L H* H%", "%L L*H %", "%L L*H %" ) mylist <- strsplit(a," ") pick <- function(x) {tail(x,2) } unlist(pick(mylist) ================================================================ --- On Fri, 6/26/09, Fredrik Karlsson <dargosch at gmail.com> wrote:> From: Fredrik Karlsson <dargosch at gmail.com> > Subject: [R] How do I get just the two last tokens of each string in a vector? > To: "R-Help List" <r-help at stat.math.ethz.ch> > Received: Friday, June 26, 2009, 7:21 AM > Dear list, > > Sorry for asking this very silly question on the list, but > I seem to > have made my life complicated by going into string > manipulation in > vectors. > What I need is to get the last part of a sting (the two > last tokens, > separated by a space), and of course, this should be done > for all > strings in a vector, creating a new vector of exual size. > > So, > > a <- c(" %L H*L L*H H%", "%L H* H%",? "%L L*H > %",???"%L L*H %" ) > > should be made into a vector > > c(" L*H H%", "H* H%",? "L*H %",???"L*H > %" ) > > I have tried strsplit, but it seems to produce a structure > I cannot > get to work in this context. Any ideas on how to solve > this? > > Thankful for all the help I can get. > > /Fredrik > > > -- > "Life is like a trumpet - if you don't put anything into > it, you don't > get anything out of it." > > ______________________________________________ > 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. >__________________________________________________________________ Make your browsing faster, safer, and easier with the new Internet Explorer? 8. Optimized for Yahoo! Get it Now for
Stavros Macrakis
2009-Jun-26 14:24 UTC
[R] How do I get just the two last tokens of each string in a vector?
One way is: a <- c(" %L H*L L*H H%", "%L H* H%", "%L L*H %", "%L L*H %" )> sub("^.*(^| )([^ ]+ [^ ]+$)","\\2",a)[1] "L*H H%" "H* H%" "L*H %" "L*H %" Just be aware that this is not terribly efficient for very large strings. -s On Fri, Jun 26, 2009 at 7:21 AM, Fredrik Karlsson<dargosch at gmail.com> wrote:> Dear list, > > Sorry for asking this very silly question on the list, but I seem to > have made my life complicated by going into string manipulation in > vectors. > What I need is to get the last part of a sting (the two last tokens, > separated by a space), and of course, this should be done for all > strings in a vector, creating a new vector of exual size. > > So, > > a <- c(" %L H*L L*H H%", "%L H* H%", ?"%L L*H %", ? "%L L*H %" ) > > should be made into a vector > > ?c(" L*H H%", "H* H%", ?"L*H %", ? "L*H %" ) > > I have tried strsplit, but it seems to produce a structure I cannot > get to work in this context. Any ideas on how to solve this? > > Thankful for all the help I can get. > > /Fredrik > > > -- > "Life is like a trumpet - if you don't put anything into it, you don't > get anything out of it." > > ______________________________________________ > 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. >
Gabor Grothendieck
2009-Jun-26 15:07 UTC
[R] How do I get just the two last tokens of each string in a vector?
Here is a similar solution using strapply in gsubfn. The pattern matches non-spaces, [^ ]+, followed by a space followed by non-spaces followed by end-of-string, $.> library(gsubfn) > strapply(a, "[^ ]+ [^ ]+$", simplify = c)[1] "L*H H%" "H* H%" "L*H %" "L*H %" On Fri, Jun 26, 2009 at 7:21 AM, Fredrik Karlsson<dargosch at gmail.com> wrote:> Dear list, > > Sorry for asking this very silly question on the list, but I seem to > have made my life complicated by going into string manipulation in > vectors. > What I need is to get the last part of a sting (the two last tokens, > separated by a space), and of course, this should be done for all > strings in a vector, creating a new vector of exual size. > > So, > > a <- c(" %L H*L L*H H%", "%L H* H%", ?"%L L*H %", ? "%L L*H %" ) > > should be made into a vector > > ?c(" L*H H%", "H* H%", ?"L*H %", ? "L*H %" ) > > I have tried strsplit, but it seems to produce a structure I cannot > get to work in this context. Any ideas on how to solve this? > > Thankful for all the help I can get. > > /Fredrik > > > -- > "Life is like a trumpet - if you don't put anything into it, you don't > get anything out of it." > > ______________________________________________ > 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. >