Hi just messing around today and am now perplexed by the seq argument "along.with". Please, I am just just seeking some knowledge here. Obviously I missed a point and would like to know what it is. seq(1,10,length.out = 2) makes sense seq(1,10,by = 2) makes sense seq(1,10, along.with = 2) What is the purpose of this argument???? per "help" page along.with take the length from the length of this argument (assuming "length" here refers to length of output). Would that not mean I should get two numbers? Regardless of the value of the along.with argument, it just returns 1. Which then leads me to be believe that the output will be the number (length) of arguments,i.e., length of the along.with argument, which indeed it does. seq(1,10,along.with = c(1,3,5,7)) yields 1 4 7 10 seq(1,10,along.with = c(111,13,5555,7) yields 1 4 7 10 seq(1,10,along.with = c("a","b","c","d")) yields 1 4 7 10 Obviously the authors of this function saw a need for this argument, but I am just not bright enough to figure out what that purpose was. It appears to me that length.out does the same thing in a much more straightforward manner, but then I am probably missing some point, subtle or otherwise. Carl Sutton ______________________________________________ [hidden email] 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. |
Have you ever used the seq_along() function?
If you want to delegate the decision of how many elements you want to process to some earlier point in your (or someone else's) code, then the most logical way to create a result vector that is the same size as some input vector, even if that vector is of zero length, is to show that vector to the seq function as an example of how long to make the result. -- Sent from my phone. Please excuse my brevity. On April 14, 2017 2:55:48 PM PDT, Carl Sutton via R-help <[hidden email]> wrote: > > >Hi > > >just messing around today and am now perplexed by the seq argument >"along.with". > > >Please, I am just just seeking some knowledge here. Obviously I missed >a point > >and would like to know what it is. > >seq(1,10,length.out = 2) makes sense >seq(1,10,by = 2) makes sense >seq(1,10, along.with = 2) What is the purpose of this argument???? > >per "help" page > along.with >take the length from the length of this argument (assuming "length" >here >refers to length of output). Would that not mean I should get two > >numbers? > > >Regardless of the value of the along.with argument, it just returns 1. >Which > >then leads me to be believe that the output will be the number (length) >of arguments,i.e., length of the along.with argument, which indeed it >does. > >seq(1,10,along.with = c(1,3,5,7)) yields 1 4 7 10 > >seq(1,10,along.with = c(111,13,5555,7) yields 1 4 7 10 > >seq(1,10,along.with = c("a","b","c","d")) yields 1 4 7 10 > > >Obviously the authors of this function saw a need for this argument, >but I am >just not bright enough to figure out what that purpose was. It appears > >to me that length.out does the same thing in a much more >straightforward >manner, but then I am probably missing some point, subtle or otherwise. > >Carl Sutton > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] 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. |
Hi Jeff
I have seen the seq_along function but never knew the what or why of it. Your response is much appreciated and just shows how brilliant the creators of R were/are. Thank you for enlightening me. Carl Sutton On Friday, April 14, 2017 3:54 PM, Jeff Newmiller <[hidden email]> wrote: Have you ever used the seq_along() function? If you want to delegate the decision of how many elements you want to process to some earlier point in your (or someone else's) code, then the most logical way to create a result vector that is the same size as some input vector, even if that vector is of zero length, is to show that vector to the seq function as an example of how long to make the result. -- Sent from my phone. Please excuse my brevity. On April 14, 2017 2:55:48 PM PDT, Carl Sutton via R-help <[hidden email]> wrote: > > >Hi > > >just messing around today and am now perplexed by the seq argument >"along.with". > > >Please, I am just just seeking some knowledge here. Obviously I missed >a point > >and would like to know what it is. > >seq(1,10,length.out = 2) makes sense >seq(1,10,by = 2) makes sense >seq(1,10, along.with = 2) What is the purpose of this argument???? > >per "help" page > along.with >take the length from the length of this argument (assuming "length" >here >refers to length of output). Would that not mean I should get two > >numbers? > > >Regardless of the value of the along.with argument, it just returns 1. >Which > >then leads me to be believe that the output will be the number (length) >of arguments,i.e., length of the along.with argument, which indeed it >does. > >seq(1,10,along.with = c(1,3,5,7)) yields 1 4 7 10 > >seq(1,10,along.with = c(111,13,5555,7) yields 1 4 7 10 > >seq(1,10,along.with = c("a","b","c","d")) yields 1 4 7 10 > > >Obviously the authors of this function saw a need for this argument, >but I am >just not bright enough to figure out what that purpose was. It appears > >to me that length.out does the same thing in a much more >straightforward >manner, but then I am probably missing some point, subtle or otherwise. > >Carl Sutton > >______________________________________________ >[hidden email] 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]] ______________________________________________ [hidden email] 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. |
Hello,
A good example of a use case of seq_along is to avoid constructs such as 1:length(x) that don't make sense and are a source for bugs whenever x is of length zero. See for instance loops where careless coders do for(i in 1:length(x)){ x[i] <- some computation } If x is of length zero the loop above will execute 2 times but the second time through it will throw an error because it will refer to x[0], which is illegal. To avoid this, use for(i in seq_along(x)){ [...] } With a zero-length x, the loop will execute zero times, the intended behaviour. If it's the first time you've came across this function, I can guarantee you that it is really, really usefull. Hope this helps, Rui Barradas Em 15-04-2017 00:58, Carl Sutton via R-help escreveu: > Hi Jeff > I have seen the seq_along function but never knew the what or why of it. Your response is much appreciated and just shows how brilliant the creators of R were/are. > Thank you for enlightening me. Carl Sutton > > On Friday, April 14, 2017 3:54 PM, Jeff Newmiller <[hidden email]> wrote: > > > Have you ever used the seq_along() function? > > If you want to delegate the decision of how many elements you want to process to some earlier point in your (or someone else's) code, then the most logical way to create a result vector that is the same size as some input vector, even if that vector is of zero length, is to show that vector to the seq function as an example of how long to make the result. > ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |