[R] RegExp question

Romain Francois romain.francois at dbmail.com
Wed Jun 16 22:15:24 CEST 2010


Le 16/06/10 18:54, Andrej a écrit :
>
> Thanks David for your fast reply, but now I realized tat "string" is
> of type:
>
>> class(string)
> [1] "jobjRef"
> attr(,"package")
> [1] "rJava"
>
> so I get an error when i try with gsub or sub:
>
>> sub("^.+\\t(\\d+)\\n.+$", "\\1", string)
> Error in as.character.default(x) :
>    no method for coercing this S4 class to a vector

"string" is a java object, you can get the character vector like this:

 > string$toString()

but I bet there are other methods in the java class that would let you 
access the information you are after without doing text demangling. Try 
completion:

 > string$<TAB>

replace <TAB> by the tab key, you should see the list of java methods 
associated with the java class. you can also call .jmethods

 > .jmethods( string )

Romain


> I think that there should be trivial solution, but... Any further
> idea?
>
> Regards, Andrej
>
>
>
> On Jun 16, 6:47 pm, David Winsemius<dwinsem... at comcast.net>  wrote:
>> On Jun 16, 2010, at 12:04 PM, Andrej wrote:
>>
>>> Dear all,
>>
>>> I'm trying to filter out the "number of leaves" (it should be 1 in the
>>> example below) from the following string:
>>
>>>> string
>>> [1] "Java-Object{J48 pruned tree\n------------------\n: 0 (15.0/3.0)\n
>>> \nNumber of Leaves  : \t1\n\nSize of the tree : \t1\n}"
>>
>>> Any idea how to do that as simple as possible? Thanks in advance for
>>> any advice.
>>
>> ?sub   # or ?gsub if you need more than one pattern matched (they are
>> on the same page).
>>
>> This should find the first occurrence of digits following a tab
>> terminated by a line feed and then return only the digits:
>>
>> string<- "Java-Object{J48 pruned tree\n------------------\n: 0
>> (15.0/3.0)\n \nNumber of Leaves  : \t1\n\nSize of the tree : \t1\n}"
>> sub("^.+\\t(\\d+)\\n.+$", "\\1", string)
>> [1] "1"
>>
>> The parens within the search pattern are matched to "\\1". Need to
>> double backslashed within patterns.
>>
>>
>>
>>> Regards, Andrej
>>
>> --
>>
>> David Winsemius, MD
>> West Hartford, CT



-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/98Uf7u : Rcpp 0.8.1
|- http://bit.ly/c6YnCi : graph gallery collage
`- http://bit.ly/bZ7ltC : inline 0.3.5



More information about the R-help mailing list