Select dataframe row containing a digit
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 at gmail.com> wrote:
? Wed, 30 Nov 2022 13:40:50 +0100 Luigi Marongiu <marongiu.luigi at 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