[R] scalar assignment within a vector within function
rui1174 at sapo.pt
Fri Mar 30 01:19:23 CEST 2012
> I'm trying to create a vector of r^2 values for using a function which I
> will run in a "for" loop. Example:
> temp.i<-fr[fr$per <=(i*.10),]
> with(temp.i, plot(x, y, main=(i*.10),))
> r2<-summary(with(temp.i, lm(y~x)))$adj.r.squared
> legend("bottomright", legend=signif(r2), col="black")
> Test comes up as the original vector of zeros. I know r2 is created for a
> couple reasons (print works, and they show up on the graphs). Also, if I
> run the function line by line with i assigned a number, test[i] is
> as it should be. However, if I call the function, plotter(i), test is not
> modified, although the r^2 prints.
> Mystified. What am I missing?
> [[alternative HTML version deleted]]
> R-help@ mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
You're changing a copy in the function's environment only, not in the
environment where it was created.
To have the changed copy, have the function return it.
(By the way, when the function returns, it rm 'temp.i', etc.)
There is a way of making global assignments but don't do this
test[i] <<- r2
without readind P. Burns, The R Inferno, Circle 6.
Hope this helps,
View this message in context: http://r.789695.n4.nabble.com/scalar-assignment-within-a-vector-within-function-tp4517310p4517387.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help