Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: R: filter string variable


From   George Vega Yon <g.vegayon@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: R: filter string variable
Date   Mon, 25 Mar 2013 09:49:53 -0300

Victor,

You should try using regular expressions, for instance

sysuse auto
keep if regexm(make, "AMC|Buick")
list make price mpg

     +------------------------------+
     | make             price   mpg |
     |------------------------------|
  1. | AMC Concord      4,099    22 |
  2. | AMC Pacer        4,749    17 |
  3. | AMC Spirit       3,799    22 |
  4. | Buick Century    4,816    20 |
  5. | Buick Electra    7,827    15 |
     |------------------------------|
  6. | Buick LeSabre    5,788    18 |
  7. | Buick Opel       4,453    26 |
  8. | Buick Regal      5,189    20 |
  9. | Buick Riviera   10,372    16 |
 10. | Buick Skylark    4,082    19 |
     +------------------------------+


will keep those obs which contain the strings AMC or Buick inside the
make variable (it is not important where the word is). Please note
that those obs which contain "amc" (lower case) will be out of your
dataset, so I recomend you to do something like this

sysuse auto
gen make_lower = lower(make)
keep if regexm(make, "amc|buick")
drop make_lower

Best wishes!

George Vega Yon
7 647 2552
http://cl.linkedin.com/in/georgevegayon


2013/3/25 Carlo Lazzaro <carlo.lazzaro@tiscalinet.it>:
> Dear Victor,
> in its triviality, the following example might hopefully help you out:
>
> -----------------------------------------------------
> set obs 10
> g Lobbists="agri" in 1/3
> replace Lobbists="farm" in 4/6
> replace Lobbists="corn" in 7
> replace Lobbists="white_collar" in 8/10
> encode Lobbists, g( Lobbists_2)
> label list Lobbists_2
> replace Lobbists_2=1 if Lobbists_2!=4
> list Lobbists_2 if Lobbists_2==1
> -----------------------------------------------------
>
> Kindest regards,
> Carlo
>
>
>
>
> -----Messaggio originale-----
> Da: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Per conto di victor
> Inviato: lunedì 25 marzo 2013 12:03
> A: statalist@hsphsun2.harvard.edu
> Oggetto: st: filter string variable
>
> Hi,
>
> I have a dataset which contains the amount of contributions from various lobbies to a member of congress. However, for my research I am only interested in the contributions from the agricultural lobby. Therefore, I would like to filter in a way that keeps only the observations which contain words like “agri” or “farm” etc. in it. Since I have to do this for various members of congress on several occasions I would like to make some sort of code for it. So far I am unable to work something out.
>
> Can anyone help me out on this?
>
> Thanks in advance,
> Victor
>
>
>
>
> --
> View this message in context: http://statalist.1588530.n2.nabble.com/filter-string-variable-tp7580328.html
> Sent from the Statalist mailing list archive at Nabble.com.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index