[R] sub question

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sat Jan 31 22:46:53 CET 2009


David Hajage wrote:
> Thank you, it's perfect.
>   

to extend the context, if you were to solve the problem in perl, the
regex below would work in perl 5.10, but not in earlier versions of
perl;  another approach is to replace the unwanted leading characters
with equally many replacement characters at once.

$string = 'aabaab';

# perl 5.10
$string =~ s/a|(*COMMIT)(*FAIL)/c/g
# $string is 'ccbaab'

# any recent perl
$string =~ s/^a*/'c' x length $&/e;
# $string is 'ccbaab'

i don't know how (if) the latter could be done in r.

vQ



> david
>
> 2009/1/30 Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no>
>
>   
>> David Hajage wrote:
>>     
>>> Hello R users,
>>>
>>> I have a string, for example:
>>> x <- "\t\tabc\t def"
>>>
>>> This string can contain any number of tabulations. I want to replace each
>>> tabulation of the begining of the string by the same number of space:
>>> "  abc\t def"
>>>
>>> I'm trying to do this with gsub :
>>>
>>>       
>>>> gsub("\t", " ", x) # replace every \t
>>>>
>>>>         
>>> [1] "  abc  def"
>>>
>>>       
>>>> sub("^\t", " ", x) # replace only the first \t
>>>>
>>>>         
>>> [1] " \tabc\t def"
>>>
>>>       
>>>> sub("^\t*", " ", x) # replace all beginning \t, but only one space
>>>>
>>>>         
>>> [1] " abc\t def"
>>>
>>> How can I do this ?
>>>
>>>
>>>       
>> there may be better solutions, but this should do:
>>
>> strings = c('pure', '\t<- 1 removed', '\t\t<- 2 removed, 1 left ->\t')
>> gsub('\t|(*COMMIT)(*FAIL)', ' ', strings, perl=TRUE)
>> # "pure" " <- 1 removed" "  <- 2 removed, 1 left ->\t"
>>
>> vQ
>>
>>
>>
>>




More information about the R-help mailing list