# How to replace a value in an R vector?

R ProgrammingServer Side ProgrammingProgramming

To replace a value in an R vector, we can use replace function. It is better to save the replacement with a new object, even if you name that new object same as the original, otherwise the replacements will not work with further analysis. As you can see in the object x5(in examples), when we replaced 5 with 3, the previous replacement of -1 with 0 returned as in original vector. Therefore, we should save it in a new object.

## Examples

Live Demo

> x1<-1:10
> x1

## Output

[1] 1 2 3 4 5 6 7 8 9 10

> replace(x1,x1==5,10)

## Output

[1] 1 2 3 4 10 6 7 8 9 10


## Example

Live Demo

> x2<-sample(0:5,50,replace=TRUE)
> x2

## Output

[1] 1 4 5 3 3 0 0 3 4 2 1 1 1 1 2 0 2 1 5 3 5 2 3 0 3 0 4 2 4 1 3 2 4 2 3 1 4 4
[39] 1 0 3 2 4 2 1 1 1 2 5 1
> replace(x2,x2==0,1)


## Output

[1] 1 4 5 3 3 1 1 3 4 2 1 1 1 1 2 1 2 1 5 3 5 2 3 1 3 1 4 2 4 1 3 2 4 2 3 1 4 4
[39] 1 1 3 2 4 2 1 1 1 2 5 1

## Example

Live Demo

> x3<-sample(25:50,100,replace=TRUE)
> x3

## Output

[1] 46 36 31 45 32 38 49 34 47 37 49 42 45 50 43 49 44 35 31 50 41 43 37 41 35
[26] 30 50 36 44 48 36 43 37 27 48 48 33 39 33 38 32 32 33 45 27 33 36 39 39 39
[51] 33 49 50 32 44 27 46 48 49 26 25 50 49 27 25 36 42 45 39 29 31 45 30 34 29
[76] 27 42 36 36 25 37 40 49 34 50 33 50 47 49 45 46 34 25 34 34 37 36 47 39 25
> replace(x3,x3==25,26)


## Output

[1] 46 36 31 45 32 38 49 34 47 37 49 42 45 50 43 49 44 35 31 50 41 43 37 41 35
[26] 30 50 36 44 48 36 43 37 27 48 48 33 39 33 38 32 32 33 45 27 33 36 39 39 39
[51] 33 49 50 32 44 27 46 48 49 26 26 50 49 27 26 36 42 45 39 29 31 45 30 34 29
[76] 27 42 36 36 26 37 40 49 34 50 33 50 47 49 45 46 34 26 34 34 37 36 47 39 26

## Example

Live Demo

> x4<-rpois(100,5)
> x4

## Output

[1] 1 4 4 3 3 3 6 4 3 2 6 5 9 3 5 4 1 1 8 5 3 5 7 8 4
[26] 3 5 6 3 4 7 1 6 5 4 7 4 4 4 4 3 5 5 1 11 4 7 3 6 3
[51] 7 2 3 7 7 6 5 7 4 2 5 5 5 8 3 5 7 2 0 8 4 3 6 2 8
[76] 7 3 9 2 4 4 4 6 5 3 4 9 7 9 6 3 11 0 7 2 4 13 8 7 5
> replace(x4,x4==13,11)


## Output

[1] 1 4 4 3 3 3 6 4 3 2 6 5 9 3 5 4 1 1 8 5 3 5 7 8 4
[26] 3 5 6 3 4 7 1 6 5 4 7 4 4 4 4 3 5 5 1 11 4 7 3 6 3
[51] 7 2 3 7 7 6 5 7 4 2 5 5 5 8 3 5 7 2 0 8 4 3 6 2 8
[76] 7 3 9 2 4 4 4 6 5 3 4 9 7 9 6 3 11 0 7 2 4 11 8 7 5

## Example

Live Demo

> x5<-round(rnorm(100,2),0)
> x5

## Output

[1] 2 2 0 1 2 3 2 2 1 2 3 1 1 0 1 2 2 3 3 2 2 3 3 2 3
[26] 1 3 1 1 4 4 2 1 3 0 1 2 2 0 2 2 1 2 4 1 -1 2 0 3 3
[51] 2 4 2 2 2 1 2 1 2 3 2 2 2 2 2 3 2 1 2 1 3 2 2 3 1
[76] 1 3 2 3 2 3 3 3 2 2 1 1 0 2 2 3 0 4 2 2 1 5 2 1 4
> replace(x5,x5==-1,0)


## Output

[1] 2 2 0 1 2 3 2 2 1 2 3 1 1 0 1 2 2 3 3 2 2 3 3 2 3 1 3 1 1 4 4 2 1 3 0 1 2
[38] 2 0 2 2 1 2 4 1 0 2 0 3 3 2 4 2 2 2 1 2 1 2 3 2 2 2 2 2 3 2 1 2 1 3 2 2 3
[75] 1 1 3 2 3 2 3 3 3 2 2 1 1 0 2 2 3 0 4 2 2 1 5 2 1 4
> replace(x5,x5==5,3)


## Output

[1] 2 2 0 1 2 3 2 2 1 2 3 1 1 0 1 2 2 3 3 2 2 3 3 2 3
[26] 1 3 1 1 4 4 2 1 3 0 1 2 2 0 2 2 1 2 4 1 -1 2 0 3 3
[51] 2 4 2 2 2 1 2 1 2 3 2 2 2 2 2 3 2 1 2 1 3 2 2 3 1
[76] 1 3 2 3 2 3 3 3 2 2 1 1 0 2 2 3 0 4 2 2 1 3 2 1 4
> replace(x5,x5==4,3)


## Output

[1] 2 2 0 1 2 3 2 2 1 2 3 1 1 0 1 2 2 3 3 2 2 3 3 2 3
[26] 1 3 1 1 3 3 2 1 3 0 1 2 2 0 2 2 1 2 3 1 -1 2 0 3 3
[51] 2 3 2 2 2 1 2 1 2 3 2 2 2 2 2 3 2 1 2 1 3 2 2 3 1
[76] 1 3 2 3 2 3 3 3 2 2 1 1 0 2 2 3 0 3 2 2 1 5 2 1 3

Saving with a new object −

## Example

> x5<-replace(x5,x5==-1,0)
> x5

## Output

[1] 2 2 0 1 2 3 2 2 1 2 3 1 1 0 1 2 2 3 3 2 2 3 3 2 3 1 3 1 1 4 4 2 1 3 0 1 2
[38] 2 0 2 2 1 2 4 1 0 2 0 3 3 2 4 2 2 2 1 2 1 2 3 2 2 2 2 2 3 2 1 2 1 3 2 2 3
[75] 1 1 3 2 3 2 3 3 3 2 2 1 1 0 2 2 3 0 4 2 2 1 5 2 1 4

## Example

-replace(x5,x5==5,3)
> x5

## Output

[1] 2 2 0 1 2 3 2 2 1 2 3 1 1 0 1 2 2 3 3 2 2 3 3 2 3 1 3 1 1 4 4 2 1 3 0 1 2
[38] 2 0 2 2 1 2 4 1 0 2 0 3 3 2 4 2 2 2 1 2 1 2 3 2 2 2 2 2 3 2 1 2 1 3 2 2 3
[75] 1 1 3 2 3 2 3 3 3 2 2 1 1 0 2 2 3 0 4 2 2 1 3 2 1 4

## Example

> x5<-replace(x5,x5==4,3)
> x5

## Output

[1] 2 2 0 1 2 3 2 2 1 2 3 1 1 0 1 2 2 3 3 2 2 3 3 2 3 1 3 1 1 3 3 2 1 3 0 1 2
[38] 2 0 2 2 1 2 3 1 0 2 0 3 3 2 3 2 2 2 1 2 1 2 3 2 2 2 2 2 3 2 1 2 1 3 2 2 3
[75] 1 1 3 2 3 2 3 3 3 2 2 1 1 0 2 2 3 0 3 2 2 1 3 2 1 3
Published on 08-Sep-2020 11:02:04