Hello, I am new to R program, therefore, I am sorry if this is a really stupid question. I wrote a simple function and for some reason it doesn't work ReturnsGrid = function(x,y,m){ for (i in 1:m){ grid[i] <- x + (i-1)*(y-x)/m } grid } xx=ReturnsGrid(0,9,3) Thanks a lot! [[alternative HTML version deleted]]
Hi, You need to create the grid object before you can assign values to it. Try ReturnsGrid = function(x,y,m){ grid <- numeric() for (i in 1:m){ grid[i] <- x + (i-1)*(y-x)/m } grid } On Fri, Nov 27, 2009 at 11:00 AM, Anastasia <nastik1 at gmail.com> wrote:> Hello, > > I am new to R program, therefore, I am sorry if this is a really stupid > question. > I wrote a simple function and for some reason it doesn't work > > ReturnsGrid = function(x,y,m){ > for (i in 1:m){ > ? grid[i] <- x + (i-1)*(y-x)/m > } > grid > } > > xx=ReturnsGrid(0,9,3) > > Thanks a lot! > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Hi, The error message, Error in grid[i] <- x + (i - 1) * (y - x)/m : object of type 'closure' is not subsettable indicates that "grid" is actually known to R as a function (type grid to see its definition). You can define your own variable with the same name, but that needs to be done before the assignment in the for loop, ReturnsGrid = function(x,y,m){ grid <- vector(length = m) for (i in 1:m){ grid[i] <- x + (i-1)*(y-x)/m } grid } ReturnsGrid(0,9,3) HTH, baptiste 2009/11/27 Anastasia <nastik1 at gmail.com>:> Hello, > > I am new to R program, therefore, I am sorry if this is a really stupid > question. > I wrote a simple function and for some reason it doesn't work > > ReturnsGrid = function(x,y,m){ > for (i in 1:m){ > ? grid[i] <- x + (i-1)*(y-x)/m > } > grid > } > > xx=ReturnsGrid(0,9,3) > > Thanks a lot! > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Hi, If you execute the following code it works but I wouldn't use grid if I were you as a vector as this name is already used by R (check help(grid)) and it explains why you have to define it in the function. ReturnsGrid = function(x,y,m){ grid <- numeric(m) for (i in 1:m){ grid[i] <- x + (i-1)*(y-x)/m } grid } xx=ReturnsGrid(0,9,3) Regards, Alain Anastasia wrote:> Hello, > > I am new to R program, therefore, I am sorry if this is a really stupid > question. > I wrote a simple function and for some reason it doesn't work > > ReturnsGrid = function(x,y,m){ > for (i in 1:m){ > grid[i] <- x + (i-1)*(y-x)/m > } > grid > } > > xx=ReturnsGrid(0,9,3) > > Thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >-- Alain Guillet Statistician and Computer Scientist SMCS - Institut de statistique - Universit? catholique de Louvain Bureau c.316 Voie du Roman Pays, 20 B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50
Hi, You would also make your code more efficient and possible more readable by doing ReturnsGrid <- function(x, y, m) { x + (seq.int(m) - 1) * (y - x) / m } (xx <- ReturnsGrid(0, 9, 3)) #[1] 0 3 6 And if you want to supply vector x and y you could do something like (there are probably better ways..) ReturnsGrid <- function(x, y, m) { if (length(x) != length(y) & (length(x)==1 | length(y) == 1)) stop ("inputs not compatible") # or something n <- max(length(x), length(y)) out <- sapply(seq.int(n), function(i) x[i] + (1:m - 1) * (y[i] - x[i]) / m) drop(out) } (xx <- ReturnsGrid(0, 9, 3)) #[1] 0 3 6 (xx <- ReturnsGrid(0:2, 9:11, 3)) #[1,] 0 1 2 #[2,] 3 4 5 #[3,] 6 7 8 But it seems like you could also do it using sequence ... seq(x, y-1, by = m) HTH, Colin -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Anastasia Sent: 27 November 2009 16:01 To: r-help at r-project.org Subject: [R] Simple Function doesn't work? Hello, I am new to R program, therefore, I am sorry if this is a really stupid question. I wrote a simple function and for some reason it doesn't work ReturnsGrid = function(x,y,m){ for (i in 1:m){ grid[i] <- x + (i-1)*(y-x)/m } grid } xx=ReturnsGrid(0,9,3) Thanks a lot! [[alternative HTML version deleted]] ______________________________________________ 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. ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email