tag:blogger.com,1999:blog-4493332405234657503.post5293946715046658032..comments2024-03-14T00:39:42.507-07:00Comments on Odd Hypothesis: RegEx: Named Capture in RLee P.http://www.blogger.com/profile/11475652437673568452noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-4493332405234657503.post-66284715356810860462012-05-03T21:35:05.366-07:002012-05-03T21:35:05.366-07:00I agree, not having a clear cut solution as is fou...I agree, not having a clear cut solution as is found in other languages like Matlab or Python makes the sub/gsub method an obvious and often suggested choice. However, it has the potential of getting pretty hairy when the number of matches to capture and their context grows.Lee P.https://www.blogger.com/profile/11475652437673568452noreply@blogger.comtag:blogger.com,1999:blog-4493332405234657503.post-10089870762026954802012-05-03T15:06:13.251-07:002012-05-03T15:06:13.251-07:00That would be pretty neat. Probably due to the la...That would be pretty neat. Probably due to the lack of a clean function, I've never even tried to use named capture in [R] and just use sub/gsub instead.<br /><br />> src = c('chA_0001', 'chA_0002', 'chA_0003', 'chB_0001', 'chB_0002', 'chB_0003')<br />> ch = sub('^ch','',sub('_[0-9]*$','',src))<br />> ids = sub('^ch.*_','',src)<br />> rv = data.frame(ch=ch, ids=ids)<br /><br />> rv<br /> ch ids<br />1 A 0001<br />2 A 0002<br />3 A 0003<br />4 B 0001<br />5 B 0002<br />6 B 0003<br />> rv$ch<br />[1] A A A B B B<br />Levels: A B<br />> rv$ids<br />[1] 0001 0002 0003 0001 0002 0003<br />Levels: 0001 0002 0003<br /><br />-SamSam Danzigerhttps://www.blogger.com/profile/16927282324472720929noreply@blogger.com