Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.A41.4.58.0401291852330.179122@homer01.u.washington.edu>
Date: 2004-01-30T02:55:22Z
From: Thomas Lumley
Subject: Variable substitution in grep pattern
In-Reply-To: <20040129190705.4993a720.gimli@email.it>

On Thu, 29 Jan 2004, Alberto Fornasier wrote:
>
> I've tried to use a "for" loop as follows:
>
> > for(i in Licenza.elenco) {
> + Licenza.elenco.prova[Licenza.elenco==i] <-
>   length(grep(".*i.*",as.character(Licenza)))}
>
> In which Licenza.elenco is a character vector containing all unique
> values I need to match (e.g. BSD License, Qt Public License (QPL), GNU
> General Public License (GPL)).
> However R handles as I expect only the first variable substitution (the
> index), but grep matches all strings containing the letter "i", that is
> 100% of the vector, except NAs of course.


You can't do that.  If you could , how would you search for all strings
containing the letter "i"?

You need to use something like paste() to construct the pattern

length(grep(paste(".*",i,".*",sep=""),as.character(Licenza)))}

	-thomas