[R] Difficultes with grep

Charles C. Berry cberry at tajo.ucsd.edu
Mon Jul 14 01:01:41 CEST 2008


On Sun, 13 Jul 2008, Fran100681 wrote:

>
> Thank you, but this is not what i want exactly.. i would want to launch
> function "myfun" with this script:
>
>> table <- sample(LETTERS[1:5], 20,TRUE)
>>
>> name <- "A"
>>
>> myfun <- function(name) {
> + r <- grep (name[^0-9], table )
...............XXXXXXXXXX............


This is not correct syntax. And likely R told you so.

If you intend "name[^0-9]" to be read as character, you need to quote it.

Perhaps you want a more complicated regex than the one Jim handed you, 
like

 	name <- "A[^0-9]"

??


> + return (r) }
>
> but if I do it ,R doesn't accept this.. i want this because i have in
> "table" (a data frame) ,a list of element that are "hsa-mir-N" (when N is
> any number)..so, if i put in argument "name" this is: "hsa-mir-70" ,function
> matches hsa-mir-70, but also (for instance) hsa-mir-700, 710 724 and so
> on... infact i insert a square brackets ^0-9 to exclude any other number
> after those i have given (in "name").. It's a little complex situation.. :(
>
> p.s: i can't put in "name" simply: hsa-mir-20 to match all hsa-mir-20 in the
> data frame because i need to match hsa-mir-20 but also (for instance)
> hsa-mir-20-3... or hsa-mir-20b, hsa-mir-20a..... and not those elements with
> another number after the 20
>
>
> jholtman wrote:
>>
>> I think this is what you want
>>
>>> table <- sample(LETTERS[1:5], 20,TRUE)
>>>
>>> name <- "A"
>>>
>>> myfun <- function(name) {
>> + r <- grep (name, table )
>> + return (r) }
>>>
>>> myfun(name)
>> [1]  4  7 14 18
>>> table
>>  [1] "E" "B" "D" "A" "B" "B" "A" "B" "E" "B" "C" "C" "C" "A" "E" "D"
>> "D" "A" "D" "C"
>>>
>>
>>
>> On Fri, Jul 11, 2008 at 1:57 PM, Fran100681 <franaruto at hotmail.it> wrote:
>>>
>>> Hello everybody!
>>>
>>> I'm using R and I have a little problem about function "grep". I 've got
>>> to
>>> make a new function in which "grep" is present. So the first argument of
>>> "grep" is the string we want to find,ok..but in this case I define a
>>> function "x" before , x receives an argument in a object "name" (for
>>> instance), then inside function "x" ,i  define a grep.. so i want to set
>>> as
>>> pattern (1st argument of grep)  what i put in "name" and not the string
>>> "name"... how do i do that?
>>>
>>> ex:
>>> name <- "Tom"
>>>
>>> myfun <- function(name) {
>>> r <- grep ("name", table )
>>> return (r) }
>>> It returns nothing because it searches the word "name" in "table" rather
>>> "Tom"...
>>> I hope to receive some little help because this is stopping me in my
>>> projcet
>>> :/ ... i m'not able to reach a solution! Thanks a lot!
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Difficultes-with-grep-tp18409347p18409347.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem you are trying to solve?
>>
>> ______________________________________________
>> 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.
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/Difficultes-with-grep-tp18409347p18428404.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list