I was recently reminded on this list that
"Using 1:ncol() is bad practice (seq_len is designed for that purpose)"
(Ripley)
This triggers the following question: What is "good practice" for
2:ncol(x)? (This is not a joke; in a recursive situation it often makes
sense to perform the calculation for the start value i = 1, then
continue with a loop over the rest, "the Fortran way";)
I usually use
if (ncol(x) > 1)
for (i in 2:ncol(x)){
....
but I can think of
for (i in seq_len(x - 1)){
I <- i + 1
....
and
i <- 1
while (i < ncol(x)){
i <- i + 1
....
What is "good practice" (efficient and safe)?
G?ran Brostr?m
seq_len and loops
5 messages · Göran Broström, Duncan Murdoch, Henrik Bengtsson +1 more
On 13-12-21 5:57 PM, G?ran Brostr?m wrote:
I was recently reminded on this list that
"Using 1:ncol() is bad practice (seq_len is designed for that purpose)"
(Ripley)
This triggers the following question: What is "good practice" for
2:ncol(x)? (This is not a joke; in a recursive situation it often makes
sense to perform the calculation for the start value i = 1, then
continue with a loop over the rest, "the Fortran way";)
I usually use
if (ncol(x) > 1)
for (i in 2:ncol(x)){
....
but I can think of
for (i in seq_len(x - 1)){
I <- i + 1
....
and
i <- 1
while (i < ncol(x)){
i <- i + 1
....
What is "good practice" (efficient and safe)?
for (i in seq_len(x - 1) + 1) should be efficient and safe. A little less efficient, but clearer would be for (i in seq_len(x)[-1]) Duncan Murdoch
On 13-12-21 6:50 PM, Duncan Murdoch wrote:
On 13-12-21 5:57 PM, G?ran Brostr?m wrote:
I was recently reminded on this list that
"Using 1:ncol() is bad practice (seq_len is designed for that purpose)"
(Ripley)
This triggers the following question: What is "good practice" for
2:ncol(x)? (This is not a joke; in a recursive situation it often makes
sense to perform the calculation for the start value i = 1, then
continue with a loop over the rest, "the Fortran way";)
I usually use
if (ncol(x) > 1)
for (i in 2:ncol(x)){
....
but I can think of
for (i in seq_len(x - 1)){
I <- i + 1
....
and
i <- 1
while (i < ncol(x)){
i <- i + 1
....
What is "good practice" (efficient and safe)?
for (i in seq_len(x - 1) + 1) should be efficient and safe.
Oops, not safe when x is 0. > A little less efficient, but clearer would be
for (i in seq_len(x)[-1]) Duncan Murdoch
What about
seq_len2 <- function(length.out, from=1L) {
seq(from=from, length.out=max(0L, length.out-from+1L))
}
lapply(0:4, FUN=seq_len2, from=2L)
[[1]] integer(0) [[2]] integer(0) [[3]] [1] 2 [[4]] [1] 2 3 [[5]] [1] 2 3 4 /Henrik
On Sat, Dec 21, 2013 at 2:57 PM, G?ran Brostr?m <goran.brostrom at umu.se> wrote:
I was recently reminded on this list that
"Using 1:ncol() is bad practice (seq_len is designed for that purpose)"
(Ripley)
This triggers the following question: What is "good practice" for 2:ncol(x)?
(This is not a joke; in a recursive situation it often makes sense to
perform the calculation for the start value i = 1, then continue with a loop
over the rest, "the Fortran way";)
I usually use
if (ncol(x) > 1)
for (i in 2:ncol(x)){
....
but I can think of
for (i in seq_len(x - 1)){
I <- i + 1
....
and
i <- 1
while (i < ncol(x)){
i <- i + 1
....
What is "good practice" (efficient and safe)?
G?ran Brostr?m
______________________________________________ R-help at r-project.org mailing list 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.
for (i in seq_len(x - 1) + 1) should be efficient and safe.
Oops, not safe when x is 0.
Also, the '+ 1' should be '+ 1L' to get the same answer as seq_len(x)[-1]. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Duncan Murdoch Sent: Saturday, December 21, 2013 3:52 PM To: G?ran Brostr?m; R-help at r-project.org Subject: Re: [R] seq_len and loops On 13-12-21 6:50 PM, Duncan Murdoch wrote:
On 13-12-21 5:57 PM, G?ran Brostr?m wrote:
I was recently reminded on this list that
"Using 1:ncol() is bad practice (seq_len is designed for that purpose)"
(Ripley)
This triggers the following question: What is "good practice" for
2:ncol(x)? (This is not a joke; in a recursive situation it often makes
sense to perform the calculation for the start value i = 1, then
continue with a loop over the rest, "the Fortran way";)
I usually use
if (ncol(x) > 1)
for (i in 2:ncol(x)){
....
but I can think of
for (i in seq_len(x - 1)){
I <- i + 1
....
and
i <- 1
while (i < ncol(x)){
i <- i + 1
....
What is "good practice" (efficient and safe)?
for (i in seq_len(x - 1) + 1) should be efficient and safe.
Oops, not safe when x is 0.
>
A little less efficient, but clearer would be
for (i in seq_len(x)[-1]) Duncan Murdoch
______________________________________________ R-help at r-project.org mailing list 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.