[R] Select dataframe row containing a digit

Luigi Marongiu m@rong|u@|u|g| @end|ng |rom gm@||@com
Wed Nov 30 14:04:59 CET 2022


Thank you,
I have been trying with [:digit:] but did not work. It worked with
`df$val[grepl('[0-9]', df$val)] = "NUM"`

On Wed, Nov 30, 2022 at 2:02 PM Ivan Krylov <krylov.r00t using gmail.com> wrote:
>
> В Wed, 30 Nov 2022 13:40:50 +0100
> Luigi Marongiu <marongiu.luigi using gmail.com> пишет:
>
> > I am formatting everything to either "POS" and "NEG",
> > but values entered as number should get the value "NUM".
> > How do I change such values?
>
> Thanks for providing an example!
>
> One idea would be to use a regular expression to locate numbers. For
> example, grepl('[0-9]', df$val) will return a logical vector indexing
> the rows containing digits. Alternatively, grepl('^[0-9.]+$', df$val,
> perl = TRUE) will index all strings consisting solely of digits and
> decimal separators.
>
> Another idea would be to parse all of the strings as numbers and filter
> out those that didn't succeed. Use as.numeric() to perform the parsing,
> suppressWarnings() to silence the messages telling you that the parsing
> failed for some of the strings and is.na() to get the logical vector
> indexing those entries that failed to parse.
>
> --
> Best regards,
> Ivan



-- 
Best regards,
Luigi



More information about the R-help mailing list