Hi All,
I have a vector of data on which I am operating. The equation with which I want to operate on the vector has a value k. I want to run the equation and output a new vector, each time replacing k with each value from the sequence I defined. I have thought about using for loops and such, but this seems like overkill. I am wondering if there is a simple solution that would allow me to accomplish this. The code I am using is outlined below. Data <- c(1:10) #Data value <- seq(from = 0, to = 100 , by = 0.01) #Sequence Data - min(Data) + k # Equation Thanks, Jake Jake Andrae PhD Candidate Geology & Geophysics � Sprigg Geobiology Centre Department of Earth Science School of Physical Sciences The University of Adelaide, AUSTRALIA 5005 Phone: 0407701565 Email: [hidden email] [[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. |
I thnk what you want is ?outer. e.g.:
outer(Data -min(Data),value,FUN = "+") Whether this works for your real task, however, may depend on details and complexities that you have omitted. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Mar 14, 2018 at 9:14 PM, Jake William Andrae < [hidden email]> wrote: > Hi All, > > I have a vector of data on which I am operating. The equation with which I > want to operate on the vector has a value k. I want to run the equation and > output a new vector, each time replacing k with each value from the > sequence I defined. I have thought about using for loops and such, but this > seems like overkill. I am wondering if there is a simple solution that > would allow me to accomplish this. > > > The code I am using is outlined below. > > > Data <- c(1:10) #Data > value <- seq(from = 0, to = 100 , by = 0.01) #Sequence > Data - min(Data) + k # Equation > > Thanks, > Jake > > > Jake Andrae > PhD Candidate > Geology & Geophysics – Sprigg Geobiology Centre > Department of Earth Science > School of Physical Sciences > The University of Adelaide, AUSTRALIA 5005 > Phone: 0407701565 > Email: [hidden email] > > > [[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. |
Thank you, I�ll give it a try.
Jake Get Outlook for iOS<https://aka.ms/o0ukef> ________________________________ From: Bert Gunter <[hidden email]> Sent: Thursday, March 15, 2018 3:23:18 PM To: Jake William Andrae Cc: R-help Subject: Re: [R] Vary an equation using values from a sequence I thnk what you want is ?outer. e.g.: outer(Data -min(Data),value,FUN = "+") Whether this works for your real task, however, may depend on details and complexities that you have omitted. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Mar 14, 2018 at 9:14 PM, Jake William Andrae <[hidden email]<mailto:[hidden email]>> wrote: Hi All, I have a vector of data on which I am operating. The equation with which I want to operate on the vector has a value k. I want to run the equation and output a new vector, each time replacing k with each value from the sequence I defined. I have thought about using for loops and such, but this seems like overkill. I am wondering if there is a simple solution that would allow me to accomplish this. The code I am using is outlined below. Data <- c(1:10) #Data value <- seq(from = 0, to = 100 , by = 0.01) #Sequence Data - min(Data) + k # Equation Thanks, Jake Jake Andrae PhD Candidate Geology & Geophysics � Sprigg Geobiology Centre Department of Earth Science School of Physical Sciences The University of Adelaide, AUSTRALIA 5005 Phone: 0407701565 Email: [hidden email]<mailto:[hidden email]> [[alternative HTML version deleted]] ______________________________________________ [hidden email]<mailto:[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 organicgeochem
It depends a bit on what you plan to do with the results. A loop would be easy and straightforward:
> k <- 1:5 > for(k in 1:5) print(Data - min(Data) + k) [1] 1 2 3 4 5 6 7 8 9 10 [1] 2 3 4 5 6 7 8 9 10 11 [1] 3 4 5 6 7 8 9 10 11 12 [1] 4 5 6 7 8 9 10 11 12 13 [1] 5 6 7 8 9 10 11 12 13 14 Or you can use sapply() to hide the loop: > sapply(1:5, function(k) Data - min(Data) + k) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 2 3 4 5 6 [3,] 3 4 5 6 7 [4,] 4 5 6 7 8 [5,] 5 6 7 8 9 [6,] 6 7 8 9 10 [7,] 7 8 9 10 11 [8,] 8 9 10 11 12 [9,] 9 10 11 12 13 [10,] 10 11 12 13 14 This is more compact if you are planning to save the results, e.g. > output <- sapply(1:5, function(k) Data - min(Data) + k) As opposed to using the loop: > output <- matrix(NA, length(k), length(Data)) for (i in seq_along(k)) > output[i, ] <- Data - min(Data) + k[i] output [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3 4 5 6 7 8 9 10 [2,] 2 3 4 5 6 7 8 9 10 11 [3,] 3 4 5 6 7 8 9 10 11 12 [4,] 4 5 6 7 8 9 10 11 12 13 [5,] 5 6 7 8 9 10 11 12 13 14 ---------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77843-4352 -----Original Message----- From: R-help <[hidden email]> On Behalf Of Jake William Andrae Sent: Wednesday, March 14, 2018 11:14 PM To: R-help <[hidden email]> Subject: [R] Vary an equation using values from a sequence Hi All, I have a vector of data on which I am operating. The equation with which I want to operate on the vector has a value k. I want to run the equation and output a new vector, each time replacing k with each value from the sequence I defined. I have thought about using for loops and such, but this seems like overkill. I am wondering if there is a simple solution that would allow me to accomplish this. The code I am using is outlined below. Data <- c(1:10) #Data value <- seq(from = 0, to = 100 , by = 0.01) #Sequence Data - min(Data) + k # Equation Thanks, Jake Jake Andrae PhD Candidate Geology & Geophysics Sprigg Geobiology Centre Department of Earth Science School of Physical Sciences The University of Adelaide, AUSTRALIA 5005 Phone: 0407701565 Email: [hidden email] [[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 |