I have mad a for loop to try and output values which i have named spectrum. However, I cannot seem to get the answers to come out as a vector which is what i need. They come out as separate values which I am then unable to join together. Thank you for(f in seq(0,0.5,0.1)) { sigmasqaured <- 1 i = complex(real = 0, imaginary = 1) spectrum <- (sigmasqaured)/(abs(1-2.7607*exp(2*pi*i*f)+3.8106*exp(4*pi*i*f)-2.6535*exp(6*pi*i*f)+0.9258*exp(8*pi*i*f))^2) print(spectrum) } [[alternative HTML version deleted]]
Instead of a for loop, why not use the vectorization inherent in R? sigmasqaured <- 1 i <- complex(real = 0, imaginary =1) f <- seq(0,0.5,0.1) spectrum <- (sigmasqaured)/(abs(1-2.7607*exp(2*pi*i*f)+3.8106*exp(4*pi*i*f)-2.6535*exp(6*pi*i*f)+0.9258*exp(8*pi*i*f))^2)> spectrum[1] 9.632720e+00 1.411130e+03 2.947753e+00 6.479994e-02 1.295175e-02 8.042731e-03 On Tue, Feb 7, 2012 at 1:08 PM, Jaymin Shah <jayminshah1@live.com> wrote:> I have mad a for loop to try and output values which i have named > spectrum. However, I cannot seem to get the answers to come out as a > vector which is what i need. They come out as separate values which I am > then unable to join together. Thank you > > for(f in seq(0,0.5,0.1)) { > sigmasqaured <- 1 > i = complex(real = 0, imaginary = 1) > spectrum <- > (sigmasqaured)/(abs(1-2.7607*exp(2*pi*i*f)+3.8106*exp(4*pi*i*f)-2.6535*exp(6*pi*i*f)+0.9258*exp(8*pi*i*f))^2) > print(spectrum) > } > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
You need to store the values of each iteration in a vector, and then display the vector after you the loop terminates. I made a few updates to your code, and it seems to do what you want now. And thanks for including the code. That made is easy to know how to help. spectrum = c() for(f in seq(0,0.5,0.1)) { sigmasqaured <- 1 i = complex(real = 0, imaginary = 1) spectrum <- c(spectrum, (sigmasqaured)/(abs(1-2.7607*exp(2*pi*i*f)+3.8106*exp(4*pi*i*f)-2.6535*exp(6*pi*i*f)+0.9258*exp(8*pi*i*f))^2)) } spectrum Andrew Miles On Feb 7, 2012, at 4:08 PM, Jaymin Shah wrote:> I have mad a for loop to try and output values which i have named spectrum. However, I cannot seem to get the answers to come out as a vector which is what i need. They come out as separate values which I am then unable to join together. Thank you > > for(f in seq(0,0.5,0.1)) { > sigmasqaured <- 1 > i = complex(real = 0, imaginary = 1) > spectrum <- (sigmasqaured)/(abs(1-2.7607*exp(2*pi*i*f)+3.8106*exp(4*pi*i*f)-2.6535*exp(6*pi*i*f)+0.9258*exp(8*pi*i*f))^2) > print(spectrum) > } > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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.[[alternative HTML version deleted]]