[R] grep

Steven Yen @tyen @end|ng |rom ntu@edu@tw
Sun Jul 14 05:23:44 CEST 2024


Yes. Any of the following worked. The pipe greater than (|>) is neat! 
Thanks.

 > v<-goprobit.p$est
 > names(v) |> grep("somewhat|very", x = _)
  [1]  6  7  8  9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 
56 57
 > v |> names() |> grep("somewhat|very", x = _)
  [1]  6  7  8  9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 
56 57
 > which(grepl("very|somewhat",names(v)))
  [1]  6  7  8  9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 
56 57
 > jj<-grep("very|somewhat",names(v)); jj
  [1]  6  7  8  9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 55 
56 57
 >

On 7/13/2024 12:31 AM, Rui Barradas wrote:
> Hello,
>
> So any of
>
>
> names(goprobit.p$est) |> grep("somewhat|very", x = _)
> goprobit.p$est |> names() |> grep("somewhat|very", x = _)
>
>
> should work, right?
>
> Hope this helps,
>
> Rui Barradas
>
> Às 16:57 de 12/07/2024, Steven Yen escreveu:
>> Thanks. First and second of the following worked, but the third (with 
>> coef) did not. This may be because I programmed my own estimation 
>> program. In short,
>>
>> names(goprobit.p$est) was recognized, but
>> names(coef(goprobit)) was not
>>
>>  > which(grepl("very|somewhat",names(goprobit.p$est)))
>>   [1]  6  7  8  9 10 11 12 13 28 29 30 31 32 33 34 35 50 51 52 53 54 
>> 55 56 57
>>  > jj<-grep("very|somewhat",names(goprobit.p$est)); length(jj)
>> [1] 24
>>  > jj<-grep("very|somewhat",names(coef(goprobit))); length(jj)
>> Error: object 'goprobit' not found
>>
>> On 7/12/2024 11:23 PM, Rui Barradas wrote:
>>>
>>> Hello,l
>>>
>>> Though the question is already answered, here is another answer to 
>>> what is 'x'.
>>> The output in the OP is not a lm or glm output but if your 
>>> regression model was programmed according to recommended practices, 
>>> there must be a 'coefficients' member in the list or object it 
>>> returns and the following should work.
>>>
>>>
>>> # this is 'x', a named character vector
>>> coef(fit)
>>> #
>>> fit |> coef() |> names() |> grep("somewhat|very", x = _)
>>>
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>>
>>> Às 10:26 de 12/07/2024, Steven Yen escreveu:
>>>> Thanks. In this case below, what is "x"? I tried rownames(out) 
>>>> which did not work.
>>>>
>>>> Sorry. Does this sound like homework to you?
>>>>
>>>> On 7/12/2024 5:09 PM, Uwe Ligges wrote:
>>>>>
>>>>>
>>>>> On 12.07.2024 10:54, Steven Yen wrote:
>>>>>> Below is part a regression printout. How can I use "grep" to 
>>>>>> identify
>>>>>> rows headed by variables (first column) with a certain label. In 
>>>>>> this
>>>>>> case, I like to find variables containing "somewhath",
>>>>>> "veryh", "somewhatm", "verym", "somewhatc", "veryc","somewhatl",
>>>>>> "veryl". The result should be an index 6:13 or 
>>>>>> 6,7,8,9,10,11,12,13. Note
>>>>>> that they all contain "somewhat" and "very". Thanks.
>>>>>
>>>>> Sounds like homework?
>>>>>
>>>>> which(grep("very|somewhat", x))
>>>>>
>>>>> Best,
>>>>> Uwe Ligges
>>>>>
>>>>>
>>>>>> est se t p g sig x.1.age 0.0341 0.0138 2.4766 0.0133 -3.8835e-04 **
>>>>>> x.1.sleep -0.1108 0.0059 -18.6277 0.0000 -4.4572e-04 *** x.1.primary
>>>>>> -0.0694 0.0289 -2.4002 0.0164 -9.9638e-06 ** x.1.middle -0.2909 
>>>>>> 0.0356
>>>>>> -8.1657 0.0000 -1.4913e-05 *** x.1.high -0.4267 0.0463 -9.2118 
>>>>>> 0.0000
>>>>>> -3.6246e-05 *** x.1.somewhath -0.6188 0.0256 -24.1971 0.0000 
>>>>>> -3.1337e-05
>>>>>> *** x.1.veryh -0.7580 0.0331 -22.8695 0.0000 -2.9558e-05 ***
>>>>>> x.1.somewhatm -0.3413 0.0426 -8.0112 0.0000 -1.8920e-05 *** 
>>>>>> x.1.verym
>>>>>> -0.3813 0.0446 -8.5413 0.0000 -4.4029e-05 *** x.1.somewhatc -0.3101
>>>>>> 0.0649 -4.7783 0.0000 -1.4353e-05 *** x.1.veryc -0.2977 0.0648 
>>>>>> -4.5910
>>>>>> 0.0000 -4.8986e-05 *** x.1.somewhatl -0.6310 0.0424 -14.8846 0.0000
>>>>>> -1.9543e-05 *** x.1.veryl -0.9132 0.0462 -19.7525 0.0000 
>>>>>> -4.4603e-05 ***
>>>>>> ...
>>>>>>
>>>>>>     [[alternative HTML version deleted]]
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help using 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 using 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.
>>>
>>>
>
>



More information about the R-help mailing list