[R] Best way to test for numeric digits?

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Thu Oct 19 01:38:48 CEST 2023


Please delete drjimlemon using bitwrit.com from your mailing list. He passed away
a month ago.
Regards,
Juel (wife)

On Thu, 19 Oct 2023, 02:09 Ben Bolker <bbolker using gmail.com wrote:

>     There are some answers on Stack Overflow:
>
>
> https://stackoverflow.com/questions/14984989/how-to-avoid-warning-when-introducing-nas-by-coercion
>
>
>
> On 2023-10-18 10:59 a.m., Leonard Mada via R-help wrote:
> > Dear List members,
> >
> > What is the best way to test for numeric digits?
> >
> > suppressWarnings(as.double(c("Li", "Na", "K",  "2", "Rb", "Ca", "3")))
> > # [1] NA NA NA  2 NA NA  3
> > The above requires the use of the suppressWarnings function. Are there
> > any better ways?
> >
> > I was working to extract chemical elements from a formula, something
> > like this:
> > split.symbol.character = function(x, rm.digits = TRUE) {
> >      # Perl is partly broken in R 4.3, but this works:
> >      regex =
> "(?<=[A-Z])(?![a-z]|$)|(?<=.)(?=[A-Z])|(?<=[a-z])(?=[^a-z])";
> >      # stringi::stri_split(x, regex = regex);
> >      s = strsplit(x, regex, perl = TRUE);
> >      if(rm.digits) {
> >          s = lapply(s, function(s) {
> >              isNotD = is.na(suppressWarnings(as.numeric(s)));
> >              s = s[isNotD];
> >          });
> >      }
> >      return(s);
> > }
> >
> > split.symbol.character(c("CCl3F", "Li4Al4H16", "CCl2CO2AlPO4SiO4Cl"))
> >
> >
> > Sincerely,
> >
> >
> > Leonard
> >
> >
> > Note:
> > # works:
> > regex = "(?<=[A-Z])(?![a-z]|$)|(?<=.)(?=[A-Z])|(?<=[a-z])(?=[^a-z])";
> > strsplit(c("CCl3F", "Li4Al4H16", "CCl2CO2AlPO4SiO4Cl"), regex, perl = T)
> >
> >
> > # broken in R 4.3.1
> > # only slightly "erroneous" with stringi::stri_split
> > regex = "(?<=[A-Z])(?![a-z]|$)|(?=[A-Z])|(?<=[a-z])(?=[^a-z])";
> > strsplit(c("CCl3F", "Li4Al4H16", "CCl2CO2AlPO4SiO4Cl"), regex, perl = T)
> >
> > ______________________________________________
> > R-help using 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.
>
> ______________________________________________
> R-help using 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list