Below...

On Sun, 23 Sep 2018, Sorkin, John wrote:

At the risk of asking something fundamental . . . .

does log(c1[-1]/c1[-len]

You dropped the closing parenthesis.

log( c1[-1] / c1[-len] )


do the following


(1) use all elements of c and perform the calculation

No. a) "c" is the base "concatenate" function, and b) it is using two different subsets of the elements in c1.

(2) delete the first element of c and perform the calculation,

It does not change c1. c1[-1] is an expression that creates an entirely new (but unnamed) vector that contains everything but the first element of c1.

(2) delete the first two elements of c and perform the calculation,

You are wandering into the weeds here...

. . .

(n) use only the last element of c and perform the calculation.

No, c1[-len] creates a temporary array that contains all elements except the one(s) in the variable "len". Note that the more conventional syntax here is c1[ length(c1) ].

c1 <- 1:3
c1[ -1 ]
#> [1] 2 3
c1[ -length(c1) ]
#> [1] 1 2
c1[ -1 ] / c1[ -length( c1 ) ] # c(2,3)/c(1,2)
#> [1] 2.0 1.5
log( c1[ -1 ] / c1[ -length( c1 ) ] ) # log( c(2, 1.5) )
#> [1] 0.6931472 0.4054651

#' Created on 2018-09-23 by the [reprex package](http://reprex.tidyverse.org) 
(v0.2.0).



Thank you,

John



John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric 
Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)



________________________________
From: R-help <r-help-boun...@r-project.org> on behalf of Wensui Liu 
<liuwen...@gmail.com>
Sent: Sunday, September 23, 2018 2:26 PM
To: Ista Zahn
Cc: r-help@r-project.org
Subject: Re: [R] For Loop

CAUTION: This message originated from a non UMB, UMSOM, FPI, or UMMS email 
system. Whether the sender is known or not known, hover over any links before 
clicking and use caution opening attachments.



what you measures is the "elapsed" time in the default setting. you
might need to take a closer look at the beautiful benchmark() function
and see what time I am talking about.

I just provided tentative solution for the person asking for it  and
believe he has enough wisdom to decide what's best. why bother to
judge others subjectively?
On Sun, Sep 23, 2018 at 1:18 PM Ista Zahn <istaz...@gmail.com> wrote:

On Sun, Sep 23, 2018 at 1:46 PM Wensui Liu <liuwen...@gmail.com> wrote:

actually, by the parallel pvec, the user time is a lot shorter. or did
I somewhere miss your invaluable insight?

c1 <- 1:1000000
len <- length(c1)
rbenchmark::benchmark(log(c1[-1]/c1[-len]), replications = 100)
                  test replications elapsed relative user.self sys.self
1 log(c1[-1]/c1[-len])          100   4.617        1     4.484    0.133
  user.child sys.child
1          0         0
rbenchmark::benchmark(pvec(1:(len - 1), mc.cores = 4, function(i) log(c1[i + 1] 
/ c1[i])), replications = 100)
                                                               test
1 pvec(1:(len - 1), mc.cores = 4, function(i) log(c1[i + 1]/c1[i]))
  replications elapsed relative user.self sys.self user.child sys.child
1          100   9.079        1     2.571    4.138      9.736     8.046

Your output is mangled in my email, but on my system your pvec
approach takes more than twice as long:

c1 <- 1:1000000
len <- length(c1)
library(parallel)
library(rbenchmark)

regular <- function() log(c1[-1]/c1[-len])
iterate.parallel <- function() {
  pvec(1:(len - 1), mc.cores = 4,
       function(i) log(c1[i + 1] / c1[i]))
}

benchmark(regular(), iterate.parallel(),
          replications = 100,
          columns = c("test", "elapsed", "relative"))
##                 test elapsed relative
## 2 iterate.parallel()   7.517    2.482
## 1          regular()   3.028    1.000

Honestly, just use log(c1[-1]/c1[-len]). The code is simple and easy
to understand and it runs pretty fast. There is usually no reason to
make it more complicated.
--Ista

On Sun, Sep 23, 2018 at 12:33 PM Ista Zahn <istaz...@gmail.com> wrote:

On Sun, Sep 23, 2018 at 10:09 AM Wensui Liu <liuwen...@gmail.com> wrote:

Why?

The operations required for this algorithm are vectorized, as are most
operations in R. There is no need to iterate through each element.
Using Vectorize to achieve the iteration is no better than using
*apply or a for-loop, and betrays the same basic lack of insight into
basic principles of programming in R.

And/or, if you want a more practical reason:

c1 <- 1:1000000
len <- 1000000
system.time( s1 <- log(c1[-1]/c1[-len]))
   user  system elapsed
  0.031   0.004   0.035
system.time(s2 <- Vectorize(function(i) log(c1[i + 1] / c1[i])) (1:len))
   user  system elapsed
  1.258   0.022   1.282

Best,
Ista


On Sun, Sep 23, 2018 at 7:54 AM Ista Zahn <istaz...@gmail.com> wrote:

On Sat, Sep 22, 2018 at 9:06 PM Wensui Liu <liuwen...@gmail.com> wrote:

or this one:

(Vectorize(function(i) log(c1[i + 1] / c1[i])) (1:len))

Oh dear god no.


On Sat, Sep 22, 2018 at 4:16 PM rsherry8 <rsher...@comcast.net> wrote:


It is my impression that good R programmers make very little use of the
for statement. Please consider  the following
R statement:
         for( i in 1:(len-1) )  s[i] = log(c1[i+1]/c1[i], base = exp(1) )
One problem I have found with this statement is that s must exist before
the statement is run. Can it be written without using a for
loop? Would that be better?

Thanks,
Bob

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

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


---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnew...@dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

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

Reply via email to