Re: [R] Simple Function doesn't work?

2009-11-27 Thread Colin Millar

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-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Colin Millar
Sent: 27 November 2009 16:41
To: Anastasia; r-help@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,]012
#[2,]345
#[3,]678


But it seems like you could also do it using sequence ...

seq(x, y-1, by = m)

HTH,
Colin 



-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Anastasia
Sent: 27 November 2009 16:01
To: r-help@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@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@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

__
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.


Re: [R] Simple Function doesn't work?

2009-11-27 Thread Colin Millar
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,]012
#[2,]345
#[3,]678


But it seems like you could also do it using sequence ...

seq(x, y-1, by = m)

HTH,
Colin 



-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Anastasia
Sent: 27 November 2009 16:01
To: r-help@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@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@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.


Re: [R] Simple Function doesn't work?

2009-11-27 Thread Alain Guillet

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

__
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.


Re: [R] Simple Function doesn't work?

2009-11-27 Thread baptiste auguie
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 :
> 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@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.
>

__
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.


Re: [R] Simple Function doesn't work?

2009-11-27 Thread Ista Zahn
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  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@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

__
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.