[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-help
Subject:    Re: [R] Select dataframe row containing a digit
From:       Rolf Turner <r.turner () auckland ! ac ! nz>
Date:       2022-11-30 19:59:28
Message-ID: 20221201085928.18573b32 () rolf-Latitude-E7470
[Download RAW message or body]


On Wed, 30 Nov 2022 13:40:50 +0100
Luigi Marongiu <marongiu.luigi@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

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic