- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to replace the values in a matrix with another value based on a condition in R?

A matrix has only numeric values and sometimes these values are either incorrectly entered or we might want to replace some of the values in a matrix based on some conditions. For example, if we have few fives in a matrix then we might want to replace all fives to an another number which is greater than 5 or less than 5.

## Example

Consider the below matrix −

set.seed(123) M <-matrix(sample(1:50,25),nrow=5) M

## Output

[,1] [,2] [,3] [,4] [,5] [1,] 31 43 27 29 36 [2,] 15 37 5 8 19 [3,] 14 48 40 41 4 [4,] 3 25 28 7 45 [5,] 42 26 9 10 17

Replacing values based on different conditions −

## Example

M[M <5]<-10 M

## Output

[,1] [,2] [,3] [,4] [,5] [1,] 31 43 27 29 36 [2,] 15 37 5 8 19 [3,] 14 48 40 41 10 [4,] 10 25 28 7 45 [5,] 42 26 9 10 17

## Example

M[M=5] <-15 M

## Output

[,1] [,2] [,3] [,4] [,5] [1,] 31 43 27 29 36 [2,] 15 37 5 8 19 [3,] 14 48 40 41 10 [4,] 10 25 28 7 45 [5,] 15 26 9 10 17

## prettyprint

M[M==5] <-15 M

## Output

[,1] [,2] [,3] [,4] [,5] [1,] 31 43 27 29 36 [2,] 15 37 15 8 19 [3,] 14 48 40 41 10 [4,] 10 25 28 7 45 [5,] 15 26 9 10 17

## Example

M[M<10&M>5]<-21 M

## Output

[,1] [,2] [,3] [,4] [,5] [1,] 31 43 27 29 36 [2,] 15 37 15 21 19 [3,] 14 48 40 41 10 [4,] 10 25 28 21 45 [5,] 15 26 21 10 17

## Example

M[M >30]<-25 M

## Output

[,1] [,2] [,3] [,4] [,5] [1,] 25 25 27 29 25 [2,] 15 25 15 21 19 [3,] 14 25 25 25 10 [4,] 10 25 28 21 25 [5,] 15 26 21 10 17

## Example

M[M==14] <-25 M

## Output

[,1] [,2] [,3] [,4] [,5] [1,] 25 25 27 29 25 [2,] 15 25 15 21 19 [3,] 25 25 25 25 10 [4,] 10 25 28 21 25 [5,] 15 26 21 10 17

## Example

M[M==17] <-15 M

## Output

[,1] [,2] [,3] [,4] [,5] [1,] 25 25 27 29 25 [2,] 15 25 15 21 19 [3,] 25 25 25 25 10 [4,] 10 25 28 21 25 [5,] 15 26 21 10 15

## Example

M[M>25&M <30]<-25 M

## Output

[,1] [,2] [,3] [,4] [,5] [1,] 25 25 25 25 25 [2,] 15 25 15 21 19 [3,] 25 25 25 25 10 [4,] 10 25 25 21 25 [5,] 15 25 21 10 15

- Related Questions & Answers
- Replace each value in a column with the largest value based on a condition in R data frame.
- How to subset a matrix based on values in a particular column in R?
- How to find the index of an element in a matrix column based on some condition in R?
- How to assign a column value in a data frame based on another column in another R data frame?
- How to subset an R data frame based on string values of a columns with OR condition?
- How to create a column with ratio of two columns based on a condition in R?
- Find the sum of a column values based on another numerical column in R.
- How to find the frequency for all columns based on a condition in R?
- How to sort a matrix based on one column in R?
- How to create a new column in an R data frame based on some condition of another column?
- Python - Replace values of a DataFrame with the value of another DataFrame in Pandas
- How to get all the values of a particular row based on a condition in a worksheet in Selenium with python?
- How to get all the values of a particular column based on a condition in a worksheet in Selenium with python?
- Replace numerical column values based on character column values in R data frame.
- How to convert a correlation matrix into a logical matrix based on correlation coefficient in R?

Advertisements