[R] Increment certain values in a vector

Sarah Goslee sarah.goslee at gmail.com
Thu Oct 1 13:01:02 CEST 2015


Hi,

On Thursday, October 1, 2015, Thomas Chesney <
Thomas.Chesney at nottingham.ac.uk> wrote:

> How can I manipulate values of only certain entries in a vector, based on
> what the existing values are?
>
> So for instance if I want to add one to each of the following values, or
> multiply them by a specific factor:
>
> agents[which(agent$membership == 1)]
>
> how would I do this please?
>
> If there was just one value to manipulate I'd just do this:
>
> value <- value + 1
>
> and this would let me set the values:
>
> agents[which(agent$membership == 1)] <- 100
>
> but how to essentially achieve this:
>
> agents[which(agent$membership == 1)] <- agents[which(agent$membership ==
> 1)] + 1


Did you try that? I'd do it without the which() because I'm lazy, but
it should work.




-- 
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org

	[[alternative HTML version deleted]]



More information about the R-help mailing list