Hi everyone,
I am taking my first R course. This was my first example. When I executed: AddLengthNoise <- function(x) {x + rnorm(length(x))} using 56 as the value of x, I expected the result to be two values, something like: [1] 56.17491697 56.02935105 because I expected rnorm to return two values and then 56 to be added to each of them. Instead, I got one value, something like: [1] 56.17491697 So I wondered how this happened and wanted to see what happens behind the scene. Coming from the Excel paradigm, I wondered, "Is there something like 'show calculation steps' in R?" So I Googled it, and got nothing related but this <https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio>. So, I tried breaking my code into separate lines and toggling breakpoints at all lines, as follows: 6| AddLengthNoise <- function(x) { - 7| x + - 8| rnorm( - 9| length( - 10| x) - 11| ) - 12| } (Where the bullet points above represent the red debugging checkpoints) Then I tried again: AddLengthNoise(56) and as I executed step by step, I could not see what I expected. I couldn't see each step's result, and I did not understand what I saw neither in the console nor in the "Traceback" window that appeared. My 2 questions: 1. Did I do something wrong? 2. Is there a way to see, like in Excel's "Show calculation steps", the result of each step alone (i.e. length(56)=2 ==> rnorm(2)={0.17491697; 0.02935105} ==> 56 + {0.17491697; 0.02935105}= ... and so on)? [[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,
First of all welcome to R, I hope you enjoy it and that as you go along it will give less and less troubles. Now, why would length(56) return 2? It's just one number, a vector of length 1. Start by trying it at an R prompt and see the result. Hope this helps, Rui Barradas Às 16:07 de 27-07-2018, إبراهيم خطاب Ibrauheem Khat'taub escreveu: > Hi everyone, > > I am taking my first R course. This was my first example. > > When I executed: > > AddLengthNoise <- function(x) {x + rnorm(length(x))} > > using 56 as the value of x, I expected the result to be two values, > something like: > > [1] 56.17491697 56.02935105 > > because I expected rnorm to return two values and then 56 to be added to > each of them. Instead, I got one value, something like: > > [1] 56.17491697 > > So I wondered how this happened and wanted to see what happens behind the > scene. Coming from the Excel paradigm, I wondered, "Is there something like > 'show calculation steps' in R?" So I Googled it, and got nothing related > but this > <https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio>. > So, I tried breaking my code into separate lines and toggling breakpoints > at all lines, as follows: > > 6| AddLengthNoise <- function(x) { > > - 7| x + > - 8| rnorm( > - 9| length( > - 10| x) > - 11| ) > - 12| } > > (Where the bullet points above represent the red debugging checkpoints) > > Then I tried again: > > AddLengthNoise(56) > > and as I executed step by step, I could not see what I expected. I couldn't > see each step's result, and I did not understand what I saw neither in the > console nor in the "Traceback" window that appeared. > > My 2 questions: > > 1. Did I do something wrong? > 2. Is there a way to see, like in Excel's "Show calculation steps", the > result of each step alone (i.e. length(56)=2 ==> rnorm(2)={0.17491697; > 0.02935105} ==> 56 + {0.17491697; 0.02935105}= ... and so on)? > > [[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. > ______________________________________________ [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. |
In reply to this post by إبراهيم خطاب Ibrauheem Khat'taub
Dear Ibrauheem
First try length(56) then try rnorm() using the value you got in stage 1 Michael On 27/07/2018 16:07, إبراهيم خطاب Ibrauheem Khat'taub wrote: > Hi everyone, > > I am taking my first R course. This was my first example. > > When I executed: > > AddLengthNoise <- function(x) {x + rnorm(length(x))} > > using 56 as the value of x, I expected the result to be two values, > something like: > > [1] 56.17491697 56.02935105 > > because I expected rnorm to return two values and then 56 to be added to > each of them. Instead, I got one value, something like: > > [1] 56.17491697 > > So I wondered how this happened and wanted to see what happens behind the > scene. Coming from the Excel paradigm, I wondered, "Is there something like > 'show calculation steps' in R?" So I Googled it, and got nothing related > but this > <https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio>. > So, I tried breaking my code into separate lines and toggling breakpoints > at all lines, as follows: > > 6| AddLengthNoise <- function(x) { > > - 7| x + > - 8| rnorm( > - 9| length( > - 10| x) > - 11| ) > - 12| } > > (Where the bullet points above represent the red debugging checkpoints) > > Then I tried again: > > AddLengthNoise(56) > > and as I executed step by step, I could not see what I expected. I couldn't > see each step's result, and I did not understand what I saw neither in the > console nor in the "Traceback" window that appeared. > > My 2 questions: > > 1. Did I do something wrong? > 2. Is there a way to see, like in Excel's "Show calculation steps", the > result of each step alone (i.e. length(56)=2 ==> rnorm(2)={0.17491697; > 0.02935105} ==> 56 + {0.17491697; 0.02935105}= ... and so on)? > > [[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. > -- Michael http://www.dewey.myzen.co.uk/home.html ______________________________________________ [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. |
In reply to this post by إبراهيم خطاب Ibrauheem Khat'taub
Debugging in R applies one statement at a time. If you want to debug within a statement you can "step into" the function calls within the statement or you can execute the function calls separately and inspect the result. Your function consists of one statement so the debugger only has one place to stop. However, once stopped, you can execute
length(x) and get a result 1 instead of 2 as you are erroneously expecting. The length function in R is NOT the equivalent of the LEN function in Excel... it tells you how many elements are in the vector, not the number of digits in a numeric or the number of characters in a string. Does AddLengthNoise( rep(56, 2) ) behave as desired? On July 27, 2018 8:07:41 AM PDT, "إبراهيم خطاب Ibrauheem Khat'taub" <[hidden email]> wrote: >Hi everyone, > >I am taking my first R course. This was my first example. > >When I executed: > >AddLengthNoise <- function(x) {x + rnorm(length(x))} > >using 56 as the value of x, I expected the result to be two values, >something like: > >[1] 56.17491697 56.02935105 > >because I expected rnorm to return two values and then 56 to be added >to >each of them. Instead, I got one value, something like: > >[1] 56.17491697 > >So I wondered how this happened and wanted to see what happens behind >the >scene. Coming from the Excel paradigm, I wondered, "Is there something >like >'show calculation steps' in R?" So I Googled it, and got nothing >related >but this ><https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio>. >So, I tried breaking my code into separate lines and toggling >breakpoints >at all lines, as follows: > >6| AddLengthNoise <- function(x) { > > - 7| x + > - 8| rnorm( > - 9| length( > - 10| x) > - 11| ) > - 12| } > >(Where the bullet points above represent the red debugging checkpoints) > >Then I tried again: > >AddLengthNoise(56) > >and as I executed step by step, I could not see what I expected. I >couldn't >see each step's result, and I did not understand what I saw neither in >the >console nor in the "Traceback" window that appeared. > >My 2 questions: > > 1. Did I do something wrong? >2. Is there a way to see, like in Excel's "Show calculation steps", the > result of each step alone (i.e. length(56)=2 ==> rnorm(2)={0.17491697; > 0.02935105} ==> 56 + {0.17491697; 0.02935105}= ... and so on)? > > [[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. -- Sent from my phone. Please excuse my brevity. ______________________________________________ [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. |
In reply to this post by إبراهيم خطاب Ibrauheem Khat'taub
You can readily do it yourself:
x <- 56 length(x) # hint: why do you expect length(56) to be 2? rnorm(length(x)) x + rnorm(length(x)) For more complicated problems, the debugger is useful, but I almost always find investigating the steps at the command line to be the most informative. Sarah On Jul 27, 2018, 9:38 AM -0600, إبراهيم خطاب Ibrauheem Khat'taub <[hidden email]>, wrote: > Hi everyone, > > I am taking my first R course. This was my first example. > > When I executed: > > AddLengthNoise <- function(x) {x + rnorm(length(x))} > > using 56 as the value of x, I expected the result to be two values, > something like: > > [1] 56.17491697 56.02935105 > > because I expected rnorm to return two values and then 56 to be added to > each of them. Instead, I got one value, something like: > > [1] 56.17491697 > > So I wondered how this happened and wanted to see what happens behind the > scene. Coming from the Excel paradigm, I wondered, "Is there something like > 'show calculation steps' in R?" So I Googled it, and got nothing related > but this > <https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio>. > So, I tried breaking my code into separate lines and toggling breakpoints > at all lines, as follows: > > 6| AddLengthNoise <- function(x) { > > - 7| x + > - 8| rnorm( > - 9| length( > - 10| x) > - 11| ) > - 12| } > > (Where the bullet points above represent the red debugging checkpoints) > > Then I tried again: > > AddLengthNoise(56) > > and as I executed step by step, I could not see what I expected. I couldn't > see each step's result, and I did not understand what I saw neither in the > console nor in the "Traceback" window that appeared. > > My 2 questions: > > 1. Did I do something wrong? > 2. Is there a way to see, like in Excel's "Show calculation steps", the > result of each step alone (i.e. length(56)=2 ==> rnorm(2)={0.17491697; > 0.02935105} ==> 56 + {0.17491697; 0.02935105}= ... and so on)? > > [[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. [[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. |
In reply to this post by Rui Barradas
Thanks a lot, Rui!
On Fri, 27 Jul 2018 at 12:02, Rui Barradas <[hidden email]> wrote: > Hello, > > First of all welcome to R, I hope you enjoy it and that as you go along > it will give less and less troubles. > > Now, why would length(56) return 2? It's just one number, a vector of > length 1. > > Start by trying it at an R prompt and see the result. > > Hope this helps, > > Rui Barradas > > Às 16:07 de 27-07-2018, إبراهيم خطاب Ibrauheem Khat'taub escreveu: > > Hi everyone, > > > > I am taking my first R course. This was my first example. > > > > When I executed: > > > > AddLengthNoise <- function(x) {x + rnorm(length(x))} > > > > using 56 as the value of x, I expected the result to be two values, > > something like: > > > > [1] 56.17491697 56.02935105 > > > > because I expected rnorm to return two values and then 56 to be added to > > each of them. Instead, I got one value, something like: > > > > [1] 56.17491697 > > > > So I wondered how this happened and wanted to see what happens behind the > > scene. Coming from the Excel paradigm, I wondered, "Is there something > like > > 'show calculation steps' in R?" So I Googled it, and got nothing related > > but this > > < > https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio > >. > > So, I tried breaking my code into separate lines and toggling breakpoints > > at all lines, as follows: > > > > 6| AddLengthNoise <- function(x) { > > > > - 7| x + > > - 8| rnorm( > > - 9| length( > > - 10| x) > > - 11| ) > > - 12| } > > > > (Where the bullet points above represent the red debugging checkpoints) > > > > Then I tried again: > > > > AddLengthNoise(56) > > > > and as I executed step by step, I could not see what I expected. I > couldn't > > see each step's result, and I did not understand what I saw neither in > the > > console nor in the "Traceback" window that appeared. > > > > My 2 questions: > > > > 1. Did I do something wrong? > > 2. Is there a way to see, like in Excel's "Show calculation steps", > the > > result of each step alone (i.e. length(56)=2 ==> > rnorm(2)={0.17491697; > > 0.02935105} ==> 56 + {0.17491697; 0.02935105}= ... and so on)? > > > > [[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. > > > [[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. |
In reply to this post by Michael Dewey-3
Thanks a lot, Michael!
On Fri, 27 Jul 2018 at 12:12, Michael Dewey <[hidden email]> wrote: > Dear Ibrauheem > > First try > > length(56) > > then try > rnorm() > using the value you got in stage 1 > > Michael > > On 27/07/2018 16:07, إبراهيم خطاب Ibrauheem Khat'taub wrote: > > Hi everyone, > > > > I am taking my first R course. This was my first example. > > > > When I executed: > > > > AddLengthNoise <- function(x) {x + rnorm(length(x))} > > > > using 56 as the value of x, I expected the result to be two values, > > something like: > > > > [1] 56.17491697 56.02935105 > > > > because I expected rnorm to return two values and then 56 to be added to > > each of them. Instead, I got one value, something like: > > > > [1] 56.17491697 > > > > So I wondered how this happened and wanted to see what happens behind the > > scene. Coming from the Excel paradigm, I wondered, "Is there something > like > > 'show calculation steps' in R?" So I Googled it, and got nothing related > > but this > > < > https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio > >. > > So, I tried breaking my code into separate lines and toggling breakpoints > > at all lines, as follows: > > > > 6| AddLengthNoise <- function(x) { > > > > - 7| x + > > - 8| rnorm( > > - 9| length( > > - 10| x) > > - 11| ) > > - 12| } > > > > (Where the bullet points above represent the red debugging checkpoints) > > > > Then I tried again: > > > > AddLengthNoise(56) > > > > and as I executed step by step, I could not see what I expected. I > couldn't > > see each step's result, and I did not understand what I saw neither in > the > > console nor in the "Traceback" window that appeared. > > > > My 2 questions: > > > > 1. Did I do something wrong? > > 2. Is there a way to see, like in Excel's "Show calculation steps", > the > > result of each step alone (i.e. length(56)=2 ==> > rnorm(2)={0.17491697; > > 0.02935105} ==> 56 + {0.17491697; 0.02935105}= ... and so on)? > > > > [[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. > > > > -- > Michael > http://www.dewey.myzen.co.uk/home.html > [[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. |
In reply to this post by Jeff Newmiller
OMG, Jeff, this is so helpful of you!
Thanks a lot! On Fri, 27 Jul 2018 at 12:32, Jeff Newmiller <[hidden email]> wrote: > Debugging in R applies one statement at a time. If you want to debug > within a statement you can "step into" the function calls within the > statement or you can execute the function calls separately and inspect the > result. Your function consists of one statement so the debugger only has > one place to stop. However, once stopped, you can execute > > length(x) > > and get a result 1 instead of 2 as you are erroneously expecting. > > The length function in R is NOT the equivalent of the LEN function in > Excel... it tells you how many elements are in the vector, not the number > of digits in a numeric or the number of characters in a string. > > Does > > AddLengthNoise( rep(56, 2) ) behave as desired? > > On July 27, 2018 8:07:41 AM PDT, "إبراهيم خطاب Ibrauheem Khat'taub" < > [hidden email]> wrote: > >Hi everyone, > > > >I am taking my first R course. This was my first example. > > > >When I executed: > > > >AddLengthNoise <- function(x) {x + rnorm(length(x))} > > > >using 56 as the value of x, I expected the result to be two values, > >something like: > > > >[1] 56.17491697 56.02935105 > > > >because I expected rnorm to return two values and then 56 to be added > >to > >each of them. Instead, I got one value, something like: > > > >[1] 56.17491697 > > > >So I wondered how this happened and wanted to see what happens behind > >the > >scene. Coming from the Excel paradigm, I wondered, "Is there something > >like > >'show calculation steps' in R?" So I Googled it, and got nothing > >related > >but this > >< > https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio > >. > >So, I tried breaking my code into separate lines and toggling > >breakpoints > >at all lines, as follows: > > > >6| AddLengthNoise <- function(x) { > > > > - 7| x + > > - 8| rnorm( > > - 9| length( > > - 10| x) > > - 11| ) > > - 12| } > > > >(Where the bullet points above represent the red debugging checkpoints) > > > >Then I tried again: > > > >AddLengthNoise(56) > > > >and as I executed step by step, I could not see what I expected. I > >couldn't > >see each step's result, and I did not understand what I saw neither in > >the > >console nor in the "Traceback" window that appeared. > > > >My 2 questions: > > > > 1. Did I do something wrong? > >2. Is there a way to see, like in Excel's "Show calculation steps", the > > result of each step alone (i.e. length(56)=2 ==> rnorm(2)={0.17491697; > > 0.02935105} ==> 56 + {0.17491697; 0.02935105}= ... and so on)? > > > > [[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. > > -- > Sent from my phone. Please excuse my brevity. > [[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. |
In reply to this post by Sarah Goslee
Thanks a lot, Sarah! Appreciate the help!
On Fri, 27 Jul 2018 at 12:55, Sarah Goslee <[hidden email]> wrote: > You can readily do it yourself: > > x <- 56 > length(x) # hint: why do you expect length(56) to be 2? > rnorm(length(x)) > x + rnorm(length(x)) > > For more complicated problems, the debugger is useful, but I almost always > find investigating the steps at the command line to be the most informative. > > Sarah > On Jul 27, 2018, 9:38 AM -0600, إبراهيم خطاب Ibrauheem Khat'taub < > [hidden email]>, wrote: > > Hi everyone, > > I am taking my first R course. This was my first example. > > When I executed: > > AddLengthNoise <- function(x) {x + rnorm(length(x))} > > using 56 as the value of x, I expected the result to be two values, > something like: > > [1] 56.17491697 56.02935105 > > because I expected rnorm to return two values and then 56 to be added to > each of them. Instead, I got one value, something like: > > [1] 56.17491697 > > So I wondered how this happened and wanted to see what happens behind the > scene. Coming from the Excel paradigm, I wondered, "Is there something like > 'show calculation steps' in R?" So I Googled it, and got nothing related > but this > < > https://support.rstudio.com/hc/en-us/articles/205612627-Debugging-with-RStudio > >. > So, I tried breaking my code into separate lines and toggling breakpoints > at all lines, as follows: > > 6| AddLengthNoise <- function(x) { > > - 7| x + > - 8| rnorm( > - 9| length( > - 10| x) > - 11| ) > - 12| } > > (Where the bullet points above represent the red debugging checkpoints) > > Then I tried again: > > AddLengthNoise(56) > > and as I executed step by step, I could not see what I expected. I couldn't > see each step's result, and I did not understand what I saw neither in the > console nor in the "Traceback" window that appeared. > > My 2 questions: > > 1. Did I do something wrong? > 2. Is there a way to see, like in Excel's "Show calculation steps", the > result of each step alone (i.e. length(56)=2 ==> rnorm(2)={0.17491697; > 0.02935105} ==> 56 + {0.17491697; 0.02935105}= ... and so on)? > > [[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. > > [[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. |
Free forum by Nabble | Edit this page |