[R] Select dataframe row containing a digit

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Wed Nov 30 14:02:42 CET 2022


В 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



More information about the R-help mailing list