[R] modifying arrays within functions
Konrad BLOCHER
kb25532 at sgh.waw.pl
Tue Feb 5 17:30:54 CET 2008
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
> }
>
> addition(X, 2)
>
> See S Poetry for more.
>
> Patrick Burns
> patrick at burns-stat.com
> +44 (0)20 8525 0696
> http://www.burns-stat.com
> (home of S Poetry and "A Guide for the Unwilling S User")
>
> 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.
>>
>>in other words
>>
>>I have an array of 1's X<-array(1,dim=c(2,2))
>>
>>I want to add a number to X[1,1] by means of a function called addition.
>>What I am writing is: addition<-function(a){X[1,1]=X[1,1}+a}
>>but it doesn't seem to work
>>
>>Any ideas? Please? :)
>>
>>Thanks
>>
>>KB
>>
>>______________________________________________
>>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.
>>
>>
>>
>>
>
More information about the R-help
mailing list