[R] Simple Function doesn't work?

Colin Millar C.Millar at MARLAB.AC.UK
Fri Nov 27 18:01:49 CET 2009


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!

	[[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

______________________________________________
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




More information about the R-help mailing list