Hi R, A quick question: How do we find the number of words in a string? Example: C="Have a nice day" And the number of words should be 4. any built in function or?... Thanks, Shubha Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San José * Singapore * www.ambaresearch.com This e-mail may contain confidential and/or privileged i...{{dropped:13}}
To put in more general, C=c("My Dog", "Its really good", "Beautiful") And the resultant output should be [1] 2 3 1 ...the number of words.... Thank you... Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: Wednesday, April 09, 2008 8:51 PM To: r-help at stat.math.ethz.ch Subject: [R] Number of words in a string Hi R, A quick question: How do we find the number of words in a string? Example: C="Have a nice day" And the number of words should be 4. any built in function or?... Thanks, Shubha Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com This e-mail may contain confidential and/or privileged i...{{dropped:13}} This e-mail may contain confidential and/or privileged i...{{dropped:10}}
Would this: sapply(strsplit(C, " "), length) work for? Markus Gesmann ?Associate Director?Libero Ventures Ltd, One Broadgate, London EC2M 2QS tel: +44 (0)207 826 9080? dir: +44 (0)207 826 9085?fax: +44 (0)207 826 9090 ?www.libero.uk.com A Lehman Brothers Company AUTHORISED AND REGULATED BY THE FINANCIAL SERVICES AUTHORITY -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: 09 April 2008 16:21 To: r-help at stat.math.ethz.ch Subject: [R] Number of words in a string Hi R, A quick question: How do we find the number of words in a string? Example: C="Have a nice day" And the number of words should be 4. any built in function or?... Thanks, Shubha Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com This e-mail may contain confidential and/or privileged i...{{dropped:13}} This message is intended for the personal and confidential use for the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction or as an official statement of Libero Ventures Ltd. Email transmissions cannot be guaranteed to be secure or error-free. Therefore we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice.
Exactly...this is what I wanted... Can we also extract/remove the last word of the strings? Example:> C=c("My Dog", "Its really good", "Beautiful") > sapply(strsplit(C, " "), length)[1] 2 3 1 Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com -----Original Message----- From: Markus Gesmann [mailto:Markus.Gesmann at libero.uk.com] Sent: Wednesday, April 09, 2008 9:00 PM To: Shubha Vishwanath Karanth; r-help at stat.math.ethz.ch Subject: RE: [R] Number of words in a string Would this: sapply(strsplit(C, " "), length) work for? Markus Gesmann ?Associate Director?Libero Ventures Ltd, One Broadgate, London EC2M 2QS tel: +44 (0)207 826 9080? dir: +44 (0)207 826 9085?fax: +44 (0)207 826 9090 ?www.libero.uk.com A Lehman Brothers Company AUTHORISED AND REGULATED BY THE FINANCIAL SERVICES AUTHORITY -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: 09 April 2008 16:21 To: r-help at stat.math.ethz.ch Subject: [R] Number of words in a string Hi R, A quick question: How do we find the number of words in a string? Example: C="Have a nice day" And the number of words should be 4. any built in function or?... Thanks, Shubha Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com This e-mail may contain confidential and/or privileged i...{{dropped:13}} This message is intended for the personal and confidential use for the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction or as an official statement of Libero Ventures Ltd. Email transmissions cannot be guaranteed to be secure or error-free. Therefore we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. Any views or opinions presented are solely those of the author and do not necessarily represent those of Amba Holdings Inc., and/or its affiliates. Important additional terms relating to this email can be obtained at http://www.ambaresearch.com/disclaimer
length(unlist(strsplit(C, ' ')))> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha > Vishwanath Karanth > Sent: Wednesday, April 09, 2008 11:21 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Number of words in a string > > Hi R, > > > > A quick question: How do we find the number of words in a string? > > > > Example: > > C="Have a nice day" > > > > And the number of words should be 4. any built in function or?... > > > > Thanks, Shubha > > Shubha Karanth | Amba Research > > Ph +91 80 3980 8031 | Mob +91 94 4886 4510 > > Bangalore * Colombo * London * New York * San Josi * > Singapore * www.ambaresearch.com > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > >
On 9 Apr 2008, at 17:29, Markus Gesmann wrote:> Would this: > > sapply(strsplit(C, " "), length) > > work for?or length(unlist(strsplit(C, " "))) --Hans
Got all the answers using ?strsplit... Is there any way without using string split?... More specifically... How can I just extract the last word in all the strings without using ?strsplit ? Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Hans-Joerg Bibiko Sent: Wednesday, April 09, 2008 9:10 PM To: r-help at stat.math.ethz.ch Subject: Re: [R] Number of words in a string On 9 Apr 2008, at 17:29, Markus Gesmann wrote:> Would this: > > sapply(strsplit(C, " "), length) > > work for?or length(unlist(strsplit(C, " "))) --Hans ______________________________________________ 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. This e-mail may contain confidential and/or privileged i...{{dropped:10}}
To put it simple, C=c("My Dog", "Its really good", "Beautiful") Now, SOMEFUNCTION(C) should give: c("My", "Its really", "") Thanks, Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: Wednesday, April 09, 2008 9:01 PM To: Markus Gesmann; r-help at stat.math.ethz.ch Subject: Re: [R] Number of words in a string Exactly...this is what I wanted... Can we also extract/remove the last word of the strings? Example:> C=c("My Dog", "Its really good", "Beautiful") > sapply(strsplit(C, " "), length)[1] 2 3 1 Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com -----Original Message----- From: Markus Gesmann [mailto:Markus.Gesmann at libero.uk.com] Sent: Wednesday, April 09, 2008 9:00 PM To: Shubha Vishwanath Karanth; r-help at stat.math.ethz.ch Subject: RE: [R] Number of words in a string Would this: sapply(strsplit(C, " "), length) work for? Markus Gesmann ?Associate Director?Libero Ventures Ltd, One Broadgate, London EC2M 2QS tel: +44 (0)207 826 9080? dir: +44 (0)207 826 9085?fax: +44 (0)207 826 9090 ?www.libero.uk.com A Lehman Brothers Company AUTHORISED AND REGULATED BY THE FINANCIAL SERVICES AUTHORITY -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha Vishwanath Karanth Sent: 09 April 2008 16:21 To: r-help at stat.math.ethz.ch Subject: [R] Number of words in a string Hi R, A quick question: How do we find the number of words in a string? Example: C="Have a nice day" And the number of words should be 4. any built in function or?... Thanks, Shubha Shubha Karanth | Amba Research Ph +91 80 3980 8031 | Mob +91 94 4886 4510 Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com This e-mail may contain confidential and/or privileged i...{{dropped:13}} This message is intended for the personal and confidential use for the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction or as an official statement of Libero Ventures Ltd. Email transmissions cannot be guaranteed to be secure or error-free. Therefore we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. Any views or opinions presented are solely those of the author and do not necessarily represent those of Amba Holdings Inc., and/or its affiliates. Important additional terms relating to this email can be obtained at http://www.ambaresearch.com/disclaimer ______________________________________________ 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. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. Any views or opinions presented are solely those of the author and do not necessarily represent those of Amba Holdings Inc., and/or its affiliates. Important additional terms relating to this email can be obtained at http://www.ambaresearch.com/disclaimer
Something like that? gsub(" {1,}\w+$", "", C)
On 9 Apr 2008, at 17:44, Shubha Vishwanath Karanth wrote:> Got all the answers using ?strsplit... Is there any way without > using string split?... More specifically... How can I just extract > the last word in all the strings without using ?strsplit ?Oops, sorry. gsub(" *\w+$", "", C) should work. --Hans
See strapply in the gsubfn package and the gsubfn home page at gsubfn.googlecode.com. Here we extract the words, the last word and all but the last word:> library(gsubfn) > strapply(C, "\\w+")[[1]] [1] "My" "Dog" [[2]] [1] "Its" "really" "good" [[3]] [1] "Beautiful"> sapply(strapply(C, "\\w+"), tail, 1)[1] "Dog" "good" "Beautiful"> sapply(strapply(C, "\\w+"), tail, -1)[[1]] [1] "Dog" [[2]] [1] "really" "good" [[3]] character(0) On Wed, Apr 9, 2008 at 11:44 AM, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Got all the answers using ?strsplit... Is there any way without using string split?... More specifically... How can I just extract the last word in all the strings without using ?strsplit ? > > Shubha Karanth | Amba Research > Ph +91 80 3980 8031 | Mob +91 94 4886 4510 > Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com > > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Hans-Joerg Bibiko > Sent: Wednesday, April 09, 2008 9:10 PM > To: r-help at stat.math.ethz.ch > > Subject: Re: [R] Number of words in a string > > > On 9 Apr 2008, at 17:29, Markus Gesmann wrote: > > Would this: > > > > sapply(strsplit(C, " "), length) > > > > work for? > > or > > length(unlist(strsplit(C, " "))) > > --Hans > > ______________________________________________ > 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. > This e-mail may contain confidential and/or privileged i...{{dropped:10}} > > > ______________________________________________ > 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. >
Actually the last example was all but the first word, not all but the last word. All but the last word is:> sapply(strapply(C, "\\w+"), head, -1)[[1]] [1] "My" [[2]] [1] "Its" "really" [[3]] character(0) On Wed, Apr 9, 2008 at 11:56 AM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> See strapply in the gsubfn package and the gsubfn home page at > gsubfn.googlecode.com. > Here we extract the words, the last word and all but the last word: > > > library(gsubfn) > > strapply(C, "\\w+") > [[1]] > [1] "My" "Dog" > > [[2]] > [1] "Its" "really" "good" > > [[3]] > [1] "Beautiful" > > > sapply(strapply(C, "\\w+"), tail, 1) > [1] "Dog" "good" "Beautiful" > > sapply(strapply(C, "\\w+"), tail, -1) > [[1]] > [1] "Dog" > > [[2]] > [1] "really" "good" > > [[3]] > character(0) > > > > On Wed, Apr 9, 2008 at 11:44 AM, Shubha Vishwanath Karanth > <shubhak at ambaresearch.com> wrote: > > Got all the answers using ?strsplit... Is there any way without using string split?... More specifically... How can I just extract the last word in all the strings without using ?strsplit ? > > > > Shubha Karanth | Amba Research > > Ph +91 80 3980 8031 | Mob +91 94 4886 4510 > > Bangalore * Colombo * London * New York * San Jos? * Singapore * www.ambaresearch.com > > > > -----Original Message----- > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Hans-Joerg Bibiko > > Sent: Wednesday, April 09, 2008 9:10 PM > > To: r-help at stat.math.ethz.ch > > > > Subject: Re: [R] Number of words in a string > > > > > > On 9 Apr 2008, at 17:29, Markus Gesmann wrote: > > > Would this: > > > > > > sapply(strsplit(C, " "), length) > > > > > > work for? > > > > or > > > > length(unlist(strsplit(C, " "))) > > > > --Hans > > > > ______________________________________________ > > 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. > > This e-mail may contain confidential and/or privileged i...{{dropped:10}} > > > > > > ______________________________________________ > > 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. > > >