R abs() is an inbuilt Math function that is used to calculate the absolute values. The abs() function returns the positive value of specific numeric data or an expression.

**R absolute value**

To calculate the absolute value in R, use the abs() method. The abs() is a built-in method that is used to get the absolute value that is the negative value is converted into a positive value and the positive value doesn’t change.

**Syntax**

```
abs(value)
```

**Parameters**

The abs() function returns the absolute value.

**Example to find absolute value in R**

```
answer1 <- abs(-21)
answer2 <- abs(19)
answer3 <- abs(-46)
answer1
answer2
answer3
```

**Output**

```
[1] 21
[1] 19
[1] 46
```

You can see that the abs() function turns a negative value into a positive value.

**Calculate the absolute value of the vector in R**

To calculate the absolute value of the vector in R, use the abs() function.

```
data1 <- abs(c(11, -21, 19))
data2 <- abs(c(-18, 29, -46))
data1
data2
```

**Output**

```
[1] 11 21 19
[1] 18 29 46
```

**Absolute value of a Matrix in R**

To calculate the absolute value of the matrix, use the R abs() method.

```
mtrx <- matrix(c(-21, 19, -51, 29, -46, 18),
nrow = 3,
ncol = 2,
byrow = TRUE)
mtrx
cat("Absolute value of matrix", "\n")
abs_mtrx <- abs(mtrx)
abs_mtrx
```

**Output**

```
[,1] [,2]
[1,] -21 19
[2,] -51 29
[3,] -46 18
Absolute value of matrix
[,1] [,2]
[1,] 21 19
[2,] 51 29
[3,] 46 18
```

**Apply abs() function to data.frame**

We can use the **abs() function **to find the absolute value of the column in the data frame.

We will create a data frame using the above **matrix** and **as.data.frame()** function.

```
mtrx <- matrix(c(-21, 19, -51, 29, -46, 18),
nrow = 3,
ncol = 2,
byrow = TRUE)
cat("The matrix is: ", "\n")
mtrx
df <- as.data.frame(mtrx)
cat("The data frame is: ", "\n")
df
abs_df <- abs(df)
cat("The absolute value of data frame is: ", "\n")
abs_df
```

**Output**

```
The matrix is:
[,1] [,2]
[1,] -21 19
[2,] -51 29
[3,] -46 18
The data frame is:
V1 V2
1 -21 19
2 -51 29
3 -46 18
The absolute value of data frame is:
V1 V2
1 21 19
2 51 29
3 46 18
```

In this example, first, we have defined a matrix and then use the **as.data.frame() **method to convert R Matrix to Data Frame and then use the abs() function to absolute value.

## Calculate Absolute Values of a data frame column in R

To calculate the absolute values of the column of the data frame, use the abs() function. We will use the above data frame to demonstrate the example.

```
mtrx <- matrix(c(-21, 19, -51, 29, -46, 18),
nrow = 3,
ncol = 2,
byrow = TRUE)
cat("The matrix is: ", "\n")
mtrx
df <- as.data.frame(mtrx)
cat("The data frame is: ", "\n")
df
df_col_abs <- df
df_col_abs$V1 <- abs(df_col_abs$V1)
df_col_abs
```

**Output**

```
[,1] [,2]
[1,] -21 19
[2,] -51 29
[3,] -46 18
The data frame is:
V1 V2
1 -21 19
2 -51 29
3 -46 18
V1 V2
1 21 19
2 51 29
3 46 18
```

That is it for the R absolute value tutorial.