Re: [R] DescTools::Quantile

2024-01-29 Thread Izmirlian, Grant (NIH/NCI) [E] via R-help
It looks like a homework assignment. It also looks like you didn't read the 
documentation carefully enough. The 'len.out' argument in seq is solely for 
specifying the length of a sequence. The 'quantile' function omputes the  
empirical quantile of raw data in the vector 'x' at cumulative 
probabilit(y)(ies) given in the weights' argument, with interpolation I'm 
between. For example

quantile(x=c(2.3, 1, 7, -4, 1), weights=c(0.60,0.45))
60%  45%
1.52 1.00

So to do what you want, there may be a canned function in R but you can always 
write your own.

First we write one that takes values in 'x' and weights in 'w', vectors of the 
same length, and returns the quantile at cumulative probability 'p' for a 'p' 
of length 1.

"%,%" <-paste0

qtl.one <-
function(p, x, w)
{
## argument checking
bad <- length(x)!=length(w)
bad <- bad || (length(p)!=1)
bad <- bad || any(diff(x)<=0)
if(bad)
  stop("Arguments 'x' and 'w' must be " %,%
  "vectors of the same legnth. Argument " %,%
  "'x' must be a vector of nondecreasing " %,%"values.")

if(any(w<=0)||(sum(w)!=1))
  stop("elements of 'w' must be positive " %,%"and sum to 1")

## the actual body of the function
x[max(which(cumsum(w)<=p))]
}

Now we write a vectorization of the above that will work given a vector of 'p' 
cumulative probabilities:


qtl <-
function(p, x, w)
{
if(length(p)==1) ans <- qtl.one(p, x, w)
if(length(p) >1)
   ans <- sapply(p, FUN=qtl.one, x=x,w=w)
ans
}


From: "Izmirlian, Grant (NIH/NCI) [E]" 
Date: Mon, Jan 29, 2024, 3:55 AM
To: "Izmirlian, Grant (NIH/NCI) [E]" 
Subject: Re: [R] DescTools::Quantile

Greetings,

I am having a problem with DescTools::Quantile
(a function computing quantiles from weighted samples):

# these sum to one
probWeights = c(
 0.0043, 0.0062, 0.0087, 0.0119, 0.0157, 0.0204, 0.0257, 0.0315, 0.0378,
 0.0441, 0.0501, 0.0556, 0.06, 0.0632, 0.0648, 0.0648, 0.0632, 0.06,
 0.0556, 0.0501, 0.0441, 0.0378, 0.0315, 0.0257, 0.0204, 0.0157, 0.0119,
 0.0087, 0.0062, 0.0043
  )
  x = seq(-100,100,length.out=length(probWeights))

  qtls <- DescTools::Quantile(x, weights=probWeights, probs=c(0.1,0.9))

cat("\nQuantiles:\n")
  print(qtls)


Both quantiles are equal to 100!
Is this function working or am I not using it correctly?

Michael Meyer

[[alternative HTML version deleted]]

__
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.


Re: [R] New line in caption with math symbols embedded in expression (paste(

2021-02-18 Thread Izmirlian, Grant (NIH/NCI) [E] via R-help
You th' barradas!!!


-Original Message-
From: Rui Barradas  
Sent: Thursday, February 18, 2021 2:10 PM
To: Izmirlian, Grant (NIH/NCI) [E] ; r-help@r-project.org
Subject: Re: [R] New line in caption with math symbols embedded in expression 
(paste(

Hello,

First of all the plotmath in your code doesn't need paste, expression alone 
will do it.

I am not sure that the following is what you want. I create the caption 
beforehand, in order to make the plotting code simpler.
The asterisks/tildes make less/more space between the text line's elements.


e <- expression(
   atop(
 P * "(" * FDP ~ ">" ~ alpha * ") " ~
   "for 'FDR' and 'Auto' FDP control method, vs '" *
   m * "' at levels of 'eff size' (col) and '" *
   p[1] * "' (row)" ~ "and",
 "a new line" ~
   bar(x) == sum(frac(x[i], n), i==1, n)
   )
)

p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + 
geom_line(aes(y=Y.p))
p <- p + labs(caption = e)
p <- p + theme(plot.caption = element_text(hjust = 0))
p


Hope this helps,

Rui Barradas
Às 17:37 de 18/02/21, Izmirlian, Grant (NIH/NCI) [E] via R-help escreveu:
> ## I am using ggplot and trying to produce a caption containing math symbols. 
> I need to
> ## add a second line. I did a fair amount of googling for answers. This one 
> seemed like
> ## it would answer my question as it is nearly exactly my problem, except 
> there is only
> ## one argument to the paste function. Note that my example is a complete 
> minimal
> ## example, just a scatterplot with a line, and the caption content seems to 
> not have
> ## anything to do with the plot. That is of course, intentional.
> ##
> ## 
> https://stackoverflow.com/questions/13223846/ggplot2-two-line-label-with-expression
> 
> library(ggplot2)
> X <- 10*runif(100)
> Y <- 2*X + rnorm(100, sd=2)
> fit <- lm(Y~X)
> Y.p <- predict(fit, newdata=data.frame(X=X))
> DAT <- data.frame(X=X)
> 
> ## without a newline
> p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
> p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
>  "for 'FDR' and 'Auto' FDP control method, vs '", m,
>  "' at levels of 'eff size' (col) and '", p[1], "' 
> (row)")))
> p <- p + theme(plot.caption = element_text(hjust = 0))
> 
> ## newline, method 1, just add a new component of paste containing "\n" 
> somewhere in the middle
> p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
> p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
>  "for 'FDR' and 'Auto' FDP control method, vs '", m,
>  "' at levels of 'eff size' (col) and '", p[1], "' 
> (row)",
>  "and \n a new line")))
> p <- p + theme(plot.caption = element_text(hjust = 0))
> 
> ## doesn't work because the newline affects only the last component of paste. 
> It looks like new line
> ## works if the line is long enough, but the newline character is being 
> parsed until the individual
> ## arguments to paste are parsed for math symbols but prior to pasting the 
> components together. prior
> ## to pasting all arguments. I can't imagine why this would be the desired 
> behavior. When the value of a
> ## caption argument is expression(paste(s1, s2, s3, ...)) then parsing for a 
> newline character should
> ## occur after the components are pasted together, right?
> 
> ## newline, method 2, enclose paste in another paste and add the new line as 
> the second argument of the
> ## outer paste
> p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
> p <- p + labs(caption=expression(paste(paste(P,"(",FDP,">", alpha,") ",
>  "for 'FDR' and 'Auto' FDP control method, vs '", m,
>  "' at levels of 'eff size' (col) and '", p[1], "' 
> (row)"),
>  "and \n a new line")))
> p <- p + theme(plot.caption = element_text(hjust = 0))
> 
> ## which has the same behavior
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> 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.
> 
__
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.


Re: [R] New line in caption with math symbols embedded in expression (paste(

2021-02-18 Thread Izmirlian, Grant (NIH/NCI) [E] via R-help
This is beautiful. Thanks!
G

-Original Message-
From: Rui Barradas  
Sent: Thursday, February 18, 2021 2:10 PM
To: Izmirlian, Grant (NIH/NCI) [E] ; r-help@r-project.org
Subject: Re: [R] New line in caption with math symbols embedded in expression 
(paste(

Hello,

First of all the plotmath in your code doesn't need paste, expression alone 
will do it.

I am not sure that the following is what you want. I create the caption 
beforehand, in order to make the plotting code simpler.
The asterisks/tildes make less/more space between the text line's elements.


e <- expression(
   atop(
 P * "(" * FDP ~ ">" ~ alpha * ") " ~
   "for 'FDR' and 'Auto' FDP control method, vs '" *
   m * "' at levels of 'eff size' (col) and '" *
   p[1] * "' (row)" ~ "and",
 "a new line" ~
   bar(x) == sum(frac(x[i], n), i==1, n)
   )
)

p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + 
geom_line(aes(y=Y.p))
p <- p + labs(caption = e)
p <- p + theme(plot.caption = element_text(hjust = 0))
p


Hope this helps,

Rui Barradas
Às 17:37 de 18/02/21, Izmirlian, Grant (NIH/NCI) [E] via R-help escreveu:
> ## I am using ggplot and trying to produce a caption containing math symbols. 
> I need to
> ## add a second line. I did a fair amount of googling for answers. This one 
> seemed like
> ## it would answer my question as it is nearly exactly my problem, except 
> there is only
> ## one argument to the paste function. Note that my example is a complete 
> minimal
> ## example, just a scatterplot with a line, and the caption content seems to 
> not have
> ## anything to do with the plot. That is of course, intentional.
> ##
> ## 
> https://stackoverflow.com/questions/13223846/ggplot2-two-line-label-with-expression
> 
> library(ggplot2)
> X <- 10*runif(100)
> Y <- 2*X + rnorm(100, sd=2)
> fit <- lm(Y~X)
> Y.p <- predict(fit, newdata=data.frame(X=X))
> DAT <- data.frame(X=X)
> 
> ## without a newline
> p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
> p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
>  "for 'FDR' and 'Auto' FDP control method, vs '", m,
>  "' at levels of 'eff size' (col) and '", p[1], "' 
> (row)")))
> p <- p + theme(plot.caption = element_text(hjust = 0))
> 
> ## newline, method 1, just add a new component of paste containing "\n" 
> somewhere in the middle
> p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
> p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
>  "for 'FDR' and 'Auto' FDP control method, vs '", m,
>  "' at levels of 'eff size' (col) and '", p[1], "' 
> (row)",
>  "and \n a new line")))
> p <- p + theme(plot.caption = element_text(hjust = 0))
> 
> ## doesn't work because the newline affects only the last component of paste. 
> It looks like new line
> ## works if the line is long enough, but the newline character is being 
> parsed until the individual
> ## arguments to paste are parsed for math symbols but prior to pasting the 
> components together. prior
> ## to pasting all arguments. I can't imagine why this would be the desired 
> behavior. When the value of a
> ## caption argument is expression(paste(s1, s2, s3, ...)) then parsing for a 
> newline character should
> ## occur after the components are pasted together, right?
> 
> ## newline, method 2, enclose paste in another paste and add the new line as 
> the second argument of the
> ## outer paste
> p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
> p <- p + labs(caption=expression(paste(paste(P,"(",FDP,">", alpha,") ",
>  "for 'FDR' and 'Auto' FDP control method, vs '", m,
>  "' at levels of 'eff size' (col) and '", p[1], "' 
> (row)"),
>  "and \n a new line")))
> p <- p + theme(plot.caption = element_text(hjust = 0))
> 
> ## which has the same behavior
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> 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.
> 
__
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.


Re: [R] New line in caption with math symbols embedded in expression (paste(

2021-02-18 Thread Izmirlian, Grant (NIH/NCI) [E] via R-help
Thank you for your suggestions. So you’re suggesting I bypass the ggplot symbol 
parsing by passing a character string to caption which has latex2exp in it. 
Good idea. So in this approach, I should break the string into new lines via 
‘atop’?
Thanks


From: Bert Gunter 
Sent: Thursday, February 18, 2021 1:47 PM
To: Izmirlian, Grant (NIH/NCI) [E] 
Cc: r-help@r-project.org
Subject: Re: [R] New line in caption with math symbols embedded in expression 
(paste(

See also this:

https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html


Bert


On Thu, Feb 18, 2021 at 10:42 AM Bert Gunter 
mailto:bgunter.4...@gmail.com>> wrote:
Note, from ?plotmath:

"Control characters (e.g., \n) are not interpreted in character strings in 
plotmath, unlike normal plotting."

For this reason, as best I can tell, you need to fool with plotmath's "atop" 
command or do separate "labs" calls.  This post seems to confirm that opinion:

https://stackoverflow.com/questions/29112697/adding-a-newline-in-a-substitute-expression

I certainly would welcome a better alternative.

Cheers,
Bert Gunter

"The trouble with having an open mind is that people keep coming along and 
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Feb 18, 2021 at 9:37 AM Izmirlian, Grant (NIH/NCI) [E] via R-help 
mailto:r-help@r-project.org>> wrote:
## I am using ggplot and trying to produce a caption containing math symbols. I 
need to
## add a second line. I did a fair amount of googling for answers. This one 
seemed like
## it would answer my question as it is nearly exactly my problem, except there 
is only
## one argument to the paste function. Note that my example is a complete 
minimal
## example, just a scatterplot with a line, and the caption content seems to 
not have
## anything to do with the plot. That is of course, intentional.
##
## 
https://stackoverflow.com/questions/13223846/ggplot2-two-line-label-with-expression

library(ggplot2)
X <- 10*runif(100)
Y <- 2*X + rnorm(100, sd=2)
fit <- lm(Y~X)
Y.p <- predict(fit, newdata=data.frame(X=X))
DAT <- data.frame(X=X)

## without a newline
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
"for 'FDR' and 'Auto' FDP control method, vs '", m,
"' at levels of 'eff size' (col) and '", p[1], "' 
(row)")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## newline, method 1, just add a new component of paste containing "\n" 
somewhere in the middle
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
"for 'FDR' and 'Auto' FDP control method, vs '", m,
"' at levels of 'eff size' (col) and '", p[1], "' 
(row)",
"and \n a new line")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## doesn't work because the newline affects only the last component of paste. 
It looks like new line
## works if the line is long enough, but the newline character is being parsed 
until the individual
## arguments to paste are parsed for math symbols but prior to pasting the 
components together. prior
## to pasting all arguments. I can't imagine why this would be the desired 
behavior. When the value of a
## caption argument is expression(paste(s1, s2, s3, ...)) then parsing for a 
newline character should
## occur after the components are pasted together, right?

## newline, method 2, enclose paste in another paste and add the new line as 
the second argument of the
## outer paste
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(paste(P,"(",FDP,">", alpha,") ",
"for 'FDR' and 'Auto' FDP control method, vs '", m,
"' at levels of 'eff size' (col) and '", p[1], "' 
(row)"),
"and \n a new line")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## which has the same behavior


[[alternative HTML version deleted]]

__
R-help@r-project.org<mailto: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.

[[alternative HTML version deleted]]

__
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.


[R] New line in caption with math symbols embedded in expression (paste(

2021-02-18 Thread Izmirlian, Grant (NIH/NCI) [E] via R-help
## I am using ggplot and trying to produce a caption containing math symbols. I 
need to
## add a second line. I did a fair amount of googling for answers. This one 
seemed like
## it would answer my question as it is nearly exactly my problem, except there 
is only
## one argument to the paste function. Note that my example is a complete 
minimal
## example, just a scatterplot with a line, and the caption content seems to 
not have
## anything to do with the plot. That is of course, intentional.
##
## 
https://stackoverflow.com/questions/13223846/ggplot2-two-line-label-with-expression

library(ggplot2)
X <- 10*runif(100)
Y <- 2*X + rnorm(100, sd=2)
fit <- lm(Y~X)
Y.p <- predict(fit, newdata=data.frame(X=X))
DAT <- data.frame(X=X)

## without a newline
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
"for 'FDR' and 'Auto' FDP control method, vs '", m,
"' at levels of 'eff size' (col) and '", p[1], "' 
(row)")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## newline, method 1, just add a new component of paste containing "\n" 
somewhere in the middle
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(P,"(",FDP,">", alpha,") ",
"for 'FDR' and 'Auto' FDP control method, vs '", m,
"' at levels of 'eff size' (col) and '", p[1], "' 
(row)",
"and \n a new line")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## doesn't work because the newline affects only the last component of paste. 
It looks like new line
## works if the line is long enough, but the newline character is being parsed 
until the individual
## arguments to paste are parsed for math symbols but prior to pasting the 
components together. prior
## to pasting all arguments. I can't imagine why this would be the desired 
behavior. When the value of a
## caption argument is expression(paste(s1, s2, s3, ...)) then parsing for a 
newline character should
## occur after the components are pasted together, right?

## newline, method 2, enclose paste in another paste and add the new line as 
the second argument of the
## outer paste
p <- ggplot(data=DAT, aes(x=X)) + geom_point(aes(y=Y)) + geom_line(aes(y=Y.p))
p <- p + labs(caption=expression(paste(paste(P,"(",FDP,">", alpha,") ",
"for 'FDR' and 'Auto' FDP control method, vs '", m,
"' at levels of 'eff size' (col) and '", p[1], "' 
(row)"),
"and \n a new line")))
p <- p + theme(plot.caption = element_text(hjust = 0))

## which has the same behavior


[[alternative HTML version deleted]]

__
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.


Re: [R] R-help Digest, Vol 212, Issue 4

2020-10-05 Thread Izmirlian, Grant (NIH/NCI) [E] via R-help
Hi -- there are lots of replies --I have not read them all, if someone else 
suggested this, sorry for duplication. This is similar to the suggestion using 
mapply, but not specific to matrices. In fact it's a kludge that applies to 
many settings. You 'sapply' over the index 1:2, and pass a, b as arguments:


a <- c(1,4)
 b <- c(5,8)

sapply(1:2, FUN=function(x, a, b)a[x]:b[x], a=a,b=b)


-Original Message-
From: r-help-requ...@r-project.org  
Sent: Monday, October 05, 2020 6:04 AM
To: r-help@r-project.org
Subject: R-help Digest, Vol 212, Issue 4

Send R-help mailing list submissions to
r-help@r-project.org

To subscribe or unsubscribe via the World Wide Web, visit
https://stat.ethz.ch/mailman/listinfo/r-help
or, via email, send a message with subject or body 'help' to
r-help-requ...@r-project.org

You can reach the person managing the list at
r-help-ow...@r-project.org

When replying, please edit your Subject line so it is more specific than "Re: 
Contents of R-help digest..."

__
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.


Re: [R] data frame solution

2019-03-20 Thread Izmirlian, Grant (NIH/NCI) [E] via R-help
Statements like c(rbind(x, xx+yy), max(t)) and rep(0,length(df$b[1]))  don't 
make any sense. You're example will be easier to understand if you show us the 
nrow(df) ==3
case. Thanks


Grant Izmirlian, Ph.D.
Mathematical Statistician
izmir...@mail.nih.gov

Delivery Address:
9609 Medical Center Dr, RM 5E130
Rockville MD 20850

Postal Address:
BG 9609 RM 5E130 MSC 9789
9609 Medical Center Dr
Bethesda, MD 20892-9789

 ofc:  240-276-7025
 cell: 240-888-7367
  fax: 240-276-7845



From: Andras Farkas 
Sent: Tuesday, March 19, 2019 7:06 AM
To: R-help Mailing List
Subject: [R] data frame solution

Hello All,

wonder if you have thoughts on a clever solution for this code:



df   <- data.frame(a = c(6,1), b = c(1000,1200), c =c(-1,3))

#the caveat here is that the number of rows for df can be anything from 1 row 
to in the hundreds. I kept it to 2 to have minimal reproducible

t<-seq(-5,24,0.1) #min(t) will always be <=df$c[1], which is the value that is 
always going to equal to min(df$c)

times1 <- c(rbind(df$c[1],df$c[1]+df$a[1]),max(t)) #length of times1 will 
always be 3, see times2 is of length 4

input1   <- c(rbind(df$b[1]/df$a[1],rep(0,length(df$b[1]))),0) #length of 
input1 will always be 3, see input2 is of length 4

out1 
<-data.frame(t,ifelse(t>=times1[1]=times1[2]=times2[1]=times2[2]=times2[3]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.


Re: [R] Q re: logical indexing with is.na

2019-03-11 Thread Izmirlian, Grant (NIH/NCI) [E] via R-help
logical indexing requires the logical index to be of the same length as the 
vector being indexed. If it is not, then the index
is wrapped to be of sufficient length. The result on line 3 is
y[c(TRUE, TRUE, FALSE, TRUE)] where the last TRUE was
originally the first component of !is.na(y[1:3])


Grant Izmirlian, Ph.D.
Mathematical Statistician
izmir...@mail.nih.gov

Delivery Address:
9609 Medical Center Dr, RM 5E130
Rockville MD 20850

Postal Address:
BG 9609 RM 5E130 MSC 9789
9609 Medical Center Dr
Bethesda, MD 20892-9789

 ofc:  240-276-7025
 cell: 240-888-7367
  fax: 240-276-7845



From: David Goldsmith 
Sent: Saturday, March 9, 2019 8:36 PM
To: r-help@r-project.org
Subject: [R] Q re: logical indexing with is.na

Hi!  Newbie (self-)learning R using P. Dalgaard's "Intro Stats w/ R"; not
new to statistics (have had grad-level courses and work experience in
statistics) or vectorized programming syntax (have extensive experience
with MatLab, Python/NumPy, and IDL, and even a smidgen--a long time ago--of
experience w/ S-plus).

In exploring the use of is.na in the context of logical indexing, I've come
across the following puzzling-to-me result:

> y; !is.na(y[1:3]); y[!is.na(y[1:3])]
[1]  0.3534253 -1.6731597 NA -0.2079209
[1]  TRUE  TRUE FALSE
[1]  0.3534253 -1.6731597 -0.2079209

As you can see, y is a four element vector, the third element of which is
NA; the next line gives what I would expect--T T F--because the first two
elements are not NA but the third element is.  The third line is what
confuses me: why is the result not the two element vector consisting of
simply the first two elements of the vector (or, if vectorized indexing in
R is implemented to return a vector the same length as the logical index
vector, which appears to be the case, at least the first two elements and
then either NA or NaN in the third slot, where the logical indexing vector
is FALSE): why does the implementation "go looking" for an element whose
index in the "original" vector, 4, is larger than BOTH the largest index
specified in the inner-most subsetting index AND the size of the resulting
indexing vector?  (Note: at first I didn't even understand why the result
wasn't simply

0.3534253 -1.6731597 NA

but then I realized that the third logical index being FALSE, there was no
reason for *any* element to be there; but if there is, due to some
overriding rule regarding the length of the result relative to the length
of the indexer, shouldn't it revert back to *something* that indicates the
"FALSE"ness of that indexing element?)

Thanks!

DLG

> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:
/Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK:
/Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] ISwR_2.0-7

loaded via a namespace (and not attached):
[1] compiler_3.5.2 tools_3.5.2

[[alternative HTML version deleted]]



[[alternative HTML version deleted]]

__
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.