R Inferno [🇷 for BE/BA]

posted by Helmut Homepage – Vienna, Austria, 2019-04-26 18:03 (1819 d 08:49 ago) – Posting: # 20244
Views: 7,468

Dear R-Users,

I can’t get my head around this:

loc.stat <- function(x, type, na.rm, distr) {
 non.numerics    <- which(is.na(suppressWarnings(as.numeric(x))))
 x[non.numerics] <- NA
 x <- as.numeric(x)
 if (distr == "nonpar") {
   switch (type,
     pct2.5  = as.numeric(quantile(x, probs=0.025, na.rm=na.rm)),
     pct25   = as.numeric(quantile(x, probs=0.25, na.rm=na.rm)),
     median  = as.numeric(median(x, na.rm=na.rm)),
     pct75   = as.numeric(quantile(x, probs=0.75, na.rm=na.rm)),
     pct97.5 = as.numeric(quantile(x, probs=0.975, na.rm=na.rm)))
 }
 if (distr == "normal") {
   mean.x <-mean(x, na.rm=na.rm)
   sd.x   <- sd(x, na.rm=na.rm)
   n.not.nas <- length(x)-length(which(is.na(x)))
   se <- sd.x/sqrt(n.not.nas)
   switch (type,
     cla2.5  = mean.x+qt(0.025, df=n.not.nas-1, lower.tail=TRUE)*se,
     ar.mean = mean.x,
     cla97.5 = mean.x+qt(0.975, df=n.not.nas-1, lower.tail=TRUE)*se)
 }
}
set.seed(123456)
x <- rnorm(12, 100, 20)
median <- loc.stat(x, "median", TRUE, "nonpar")
mean   <- loc.stat(x, "ar.mean", TRUE, "normal")


Gives

R> median
NULL
R> mean
[1] 109.7066


Why the heck? Quoting the R-Inferno:

Curly braces are also useful with loops, switch and if.

What? How? Google and R-help were not my friends.

Dif-tor heh smusma 🖖🏼 Довге життя Україна! [image]
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes

Complete thread:

UA Flag
Activity
 Admin contact
22,986 posts in 4,823 threads, 1,671 registered users;
87 visitors (0 registered, 87 guests [including 8 identified bots]).
Forum time: 02:52 CEST (Europe/Vienna)

The only way to comprehend what mathematicians mean by Infinity
is to contemplate the extent of human stupidity.    Voltaire

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5