Two things:

It's just a guess as to what your problem is, but functions in R don't
usually act on the object that is passed to them, but rather a copy thereof:
if you want to keep the values calculated within the function, that's
usually done with an assignment statement combined with the function call.

E.g.

squaring <- function(x) {x^2}

>z = 1:5
>squaring(z)
>z
[1] 1 2 3 4 5
> z = squaring(z)
> z
[1] 1 4 9 16 25

More generally, please provide code as requested in the posting guide.

Hope this helps,

Michael Weylandt


On Sun, Aug 28, 2011 at 2:39 PM, DimmestLemming <NICOADAMS000@gmail.com>wrote:

> I'm somewhat new to R, but I've had a lot of experience in Java.
>
> I'm working on a function that takes data from a data frame, does some math
> and assigns the values to a vector. Pretty simple. I plan to merge the
> vector with the data frame when I'm done.
>
> The vector is called offense1 (there will eventually be 2). I declared it
> on
> its own, outside of the function. Right now its values are the same as its
> indices: 1, 2, 3... Then I plugged it into the function: getOffense1(1:20,
> offense1). 1:20 is the range of offense1 that will be worked upon.
>
> Everything about the function works: It generates the correct values and
> plugs them into the vector. I checked with print statements. The problem is
> that when the function ends, offense1 is unchanged. All the values it takes
> on are temporary.
>
> I thought it might be because the assignment (offense1[v] <- ___) happens
> inside a for loop, but a call to
> print( offense1[1:20] )
> just after the loop is finished turns out the right values. All that seems
> to matter is whether it's inside the function or not.
>
> How can I assign values permanently to offense1?
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Function-won-t-permanently-assign-values-to-a-vector-tp3774850p3774850.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]

