[R] Select dataframe row containing a digit
Rolf Turner
r@turner @end|ng |rom @uck|@nd@@c@nz
Wed Nov 30 20:59:28 CET 2022
On Wed, 30 Nov 2022 13:40:50 +0100
Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
> Hello,
> I have a data frame where some lines containing strings including
> digits. How do I select those rows and change their values?
>
> In essence, I have a data frame with different values assigned to the
> column "val". 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?
>
What I do in such circumstances:
suppressWarnings(X$val[!is.na(as.numeric(X$val))] <- "NUM")
The "suppressWarnings()" bit is just included due to my OCD.
This avoids fooling about with regular expressions, which always
requires a huge amount of trial and error, and a great diminishment of
the amount of hair on one's head (as a result of tearing out).
Note that I have changed the name of your data frame from "df" to "X",
since df() is a built-in R function (density of the F-distribution).
See fortunes::fortune("might clash").
cheers,
Rolf Turner
--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
More information about the R-help
mailing list