s function in R: Handle the non-number values in the object

s function in R

In this article, we will show you what s function in R is and how to use. The s function can help ignore or handle the non-number value and the NA value. Let’s follow this article to learn what the s function in R is and how to perform the function with the explanation and examples below.

What does the s function do in R?

The s function in R is built-in in the ‘hablar’ package, it is the simple function that can help you handle the non-number value such as Inf, -Inf, or NaN value. The function will replace or remove it from the vector, the array, the matrix, or the data frame,… However, the function will return NA if the length of the object is 0. Let’s learn about the syntax of the s function in the next title below.

Syntax:

hablar :: s(.x, ignore_na = TRUE)

Parameters:

  • .x: The vector, the array, the matrix or the data frame.
  • ignore_na: The default is TRUE. Remove NA value in the result or not. If FALSE, the function will convert the Inf, -Inf value to NA value.

After learning what the s function in R is and the syntax of the s function, you will learn how to use it in the next title below.

How to use the s function in R?

The s function is used to handle the Inf, -Inf, or the NA value in R. It will remove it or handle it from the result. 

But first, you have to install the ‘hablar’ package to work with it.

Install the ‘hablar’ package

You can install the ‘hablar’ package by the following command below.

install.packages('hablar')

Use the s function with the vector

You can use the s function with the vector to handle or remove the non-number values.

Look at the example below.

# Create the vector.
vec <- c (1,2,3,4,5,NA,Inf,-Inf)

# Remove the non-number value by the s function.
hablar::s(vec)

Output

[1] 1 2 3 4 5

Convert all the non-number values to NA by the s function

You can use the s function to convert all the non-number values in the object to the NA value by setting ignore_na = FALSE.

Look at the example below.

# Create the vector.
vec <- c (1,2,3,4,5,NA,Inf,-Inf)

# Convert Inf, -Inf to NA.
hablar::s(vec, ignore_na=FALSE)

Output

[1]  1  2  3  4  5 NA NA NA

Use the s function with the data frame

You can use the s function with the vector to handle the non-number in the data frame.

Look at the example below.

# Create the data frame.
df <- data.frame( name = c ('Ronaldo', 'Messi', 'M3P', 'Giroud', 'Maguire', 'Havert', 'Kai'),
		  GA  = c (1150, 1250, 300,300, NA, Inf, -Inf) )

# Remove the non-number value by the s function.
hablar::s(df, ignore_na = FALSE)

Output

      name   GA
1 Ronaldo  1150
2 Messi    1250
3 M3P       300
4 Giroud    300
5 Maguire    NA
6 Havert     NA
7 Kai        NA

Not that, if you set ignore_na = TRUE, the function will return the vector of all elements without NA value. Because if you set ignore_na = TRUE, the function will remove all the non-number values so the number of the elements in each row is not equal.

Summary

You have learned about what the s function in R is and how to use. By this function, you can handle the non-number values in the object. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *