Skip to content

Simple Function doesn't work?

6 messages · Anastasia, Ista Zahn, Baptiste Auguie +2 more

#
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:

  
    
#
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>:
#
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:

  
    
#
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!


______________________________________________
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
#
Erm... Maybe the sequence bit wont work ... A bit hasty there

And also the length check should be

if (length(x) != length(y) & !(length(x) == 1 | length(y) == 1)) stop
("inputs not compatible") # or something
 
I missed out the not!

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Colin Millar
Sent: 27 November 2009 16:41
To: Anastasia; r-help at r-project.org
Subject: Re: [R] Simple Function doesn't work?

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!


______________________________________________
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

______________________________________________
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
______________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email