Re: [R] modifying arrays within functions

From: Konrad BLOCHER <kb25532_at_sgh.waw.pl>
Date: Tue, 5 Feb 2008 17:32:26 +0100 (CET)

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<-array(1,dim=c(2,2))
> addition<-function(a){
> X[1,1] <<- X[1,1]+a # not '='
> }
> addition(1)
> X
>
>
> [,1] [,2]
> [1,] 2 1
> [2,] 1 1
>
>
> Nevertheless I would advise against writing and using functions that
> have side effects outside their scope.
> It is in general dangerous.
>
> Best,
>
> Martin
>
> ______________________________________________
> 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.
>



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. Received on Tue 05 Feb 2008 - 16:34:46 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Feb 2008 - 17:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive