Bill Venables.
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Martin Elff
Sent: Wednesday, 6 February 2008 2:23 AM
To: r-help@r-project.org
Subject: Re: [R] modifying arrays within functions
On Tuesday 05 February 2008 (16:51:41), Konrad BLOCHER wrote
Got it Thanks
> On Tuesday 05 February 2008 (16:51:41), Konrad BLOCHER wrote:
>> Hi,
>>
>> I'm pretty new to R and seem to be having difficulties with writing a
>> function that would change an array and keep the change after the
>> function
>> finishes its work.
>>
> It seems you want this:
>
> X
In general functions changing global variables as a side effect is dangerous
(i.e., often leads to programming errors and difficult-to-maintain code).
So, if the performance of the following is sufficient, it is what I would
recommend.
addition <- function(X, a) {
X[1, 1] <- X[1, 1] + a
X
Thanks that works fine but still doesnt keep the result only prints it.
How can I make it retain the value in the variable?
Thanks,
> You want to have X as an argument to your function
> and return X at the end of it:
>
> addition <- function(x, a) {
> x[1,1] <- x[1,1] + a
> x
> }
>
> add
On Tuesday 05 February 2008 (16:51:41), Konrad BLOCHER wrote:
> Hi,
>
> I'm pretty new to R and seem to be having difficulties with writing a
> function that would change an array and keep the change after the function
> finishes its work.
>
It seems you want this:
X<-array(1,dim=c(2,2))
addition<
Hi KB
I am not sure exactly what you want to do but perhaps this is this
closer to what you need:
addition<-function(X, a){Xnew<-X + a}
X<-array(1,dim=c(2,2))
a<-2
Xa<-addition(X,a)
Xa
Regards
JS
---
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf O
6 matches
Mail list logo