On 01/03/2014 23:32, Hui Du wrote:> Hi All,
>
> My sample code looks like
>
> options(stringsAsFactors = FALSE);
> clean = function(x)
> {
> loc = agrep("ABC", x$name);
> x[loc,]$new_name <- "NEW";
> x;
> }
>
> name = c("12", "dad", "dfd");
> y = data.frame(name = as.character(name), idx = 1:3);
> y$new_name = y$name;
>
> z <- clean(y)
>
> The snippet does not work because I forgot to test the return value of
agrep. If no pattern is found, it returns 0 and the following x[loc, ]$new_name
does not like. I know how to fix that part. However, my code has many places
like that, say over 100 calls for agrep or grep for different patterns and
substitution. Is there any smart way to fix them all rather than line by line?
That is not true: it returns integer(0). (If it returned 0 it would work.)
For grep() I would recommend using grepl() instead. Otherwise
if(length(loc)) x[loc,]$new_name <- "NEW"
or
x[loc,]$new_name <- rep_len("NEW", length(loc))
Your code is full of pointless empty statements (between ; and NL): R is
not C and ; is a separator, not a terminator.
> Many thanks.
>
> HXD
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595