[R] subbing a string vector for another string vector
David Winsemius
dwinsemius at comcast.net
Wed Sep 8 23:13:47 CEST 2010
On Sep 8, 2010, at 4:45 PM, jlemaitre wrote:
>
> I have a data frame with two columns:
> url.img patt.url
> 1 http://$IMAGE_ID$ www.url.com/image.jpg
> 2 $IMAGE_ID$ http://www.blah.com/image.gif
> ...
>
> I want to replace "$IMAGE_ID$" with the corresponding entry in the
> pattern
> column such that the result would appear as follows:
A) Drop the use of the name "pattern" because it is the argument name
for regex functions. Name it something else like "patt.url".
Furthermore "image" is also a function name so use somehting more
specific there too, say "url.img"
B) use grep(<grep-pattern>, df$url.img) to identify the rows of
"patt.url" you want to assemble.
Perhaps:
df[ grep("\\$IMAGE\\_ID\\$", df$url.img), "patt.url"]
>
> url
> http://www.url.com/image.jpg
> http://www.blah.com/image.gif
>
> Using something like > gsub(,image,pattern) doesn't work
> because it only takes uses the first entry in "pattern" as the
> replacement
> for all "image" entries:
>
> url
> http://www.url.com/image.jpg
> www.url.com/image.jpg
>
> Please help.
> Thanks.
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/subbing-a-string-vector-for-another-string-vector-tp2532005p2532005.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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list