banner



How To Remove Null Values In R

The is.null Function in R (4 Examples)

Basic R Syntax:

The R function is.zip indicates whether a data object is of the information type NULL (i.e. a missing value). The function returns TRUE in case of a NULL object and Imitation in case that the data object is not Zilch. The code above illustrates how to use is.nil in R.

In the post-obit article, I'll provide you with iv examples for the application of the is.null function in R. Allow's swoop in…

Example 1: Check if Object is NULL in R

Consider the following instance vector in R:

x1                <-                c(                3,                7,                1,                5,                ii,                8                )                # Create vector in R              

x1 <- c(3, 7, 1, 5, 2, 8) # Create vector in R

By applying the is.naught function nosotros can bank check whether this information object is Zippo. Every bit we know, information technology is not, and therefore the is.zilch office returns False:

                is                .                null                (x1)                # Check if vector is NULL                # FALSE              

is.null(x1) # Check if vector is Naught # FALSE

Now, consider the post-obit NULL object in R:

x2                <-                NULL                # Create NULL object in R              

x2 <- Zippo # Create NULL object in R

In this case, the is.null function returns TRUE, indicating that the object x2 is NULL:

                is                .                null                (x2)                # Check if object is Nil                # TRUE              

is.goose egg(x2) # Bank check if object is NULL # Truthful

Example 2: Check if Object is not Cypher

The is.null function can be used the other way around in order to check whether a information object is not Nil. You simply have to put an explanation mark in front of is.null (i.e. !is.aught):

                !                is                .                nothing                (x1)                # Cheque if vector is not NULL                # Truthful                !                is                .                null                (x2)                # Bank check if object is not Goose egg                # FALSE              

!is.zilch(x1) # Check if vector is not NULL # True !is.goose egg(x2) # Check if object is non NULL # Fake

Practise you need more explanations on Examples one and ii of this page? And so check out the following video of my YouTube channel:

Example 3: Check if Data Frame is Zippo

The aforementioned principle can be applied to a information frame. Let'due south load some case data to RStudio:

data(                "mtcars"                )                # Load mtcars data fix                head(mtcars)                # Kickoff vi rows of mtcars data              

information("mtcars") # Load mtcars information prepare caput(mtcars) # Commencement 6 rows of mtcars data

Data Frame Example for Applying the is.null R Function

Table 1: First six Rows of the Example Data Fix mtcars.

Now, let'due south cheque whether the mtcars data is a Zip object (obviously it's not):

                is                .                goose egg                (mtcars)                # Check if information frame is NULL                # FALSE              

is.nada(mtcars) # Cheque if data frame is NULL # Faux

However, if nosotros catechumen this information matrix to a NULL object, the is.nothing function returns TRUE:

mtcars2                <-                mtcars                # Replicate mtcars data frame                mtcars2                <-                NULL                # Convert mtcars2 data to Nil object                is                .                null                (mtcars2)                # TRUE              

mtcars2 <- mtcars # Replicate mtcars data frame mtcars2 <- Nix # Convert mtcars2 information to NULL object is.null(mtcars2) # TRUE

Example 4: Check if List is NULL

Equally yous have seen, the is.null office can exist applied to a diversity of data classes and formats. This also includes list objects in R. Consider the following example list:

mylist                <-                listing(                )                # Empty list object                mylist[                [                1                ]                ]                <-                x1                # Assign x1 example vector to offset listing entry                mylist[                [                2                ]                ]                <-                mtcars[                1                :                iii,                3                :                5                ]                # Assign mtcars subset to second listing entry              

mylist <- list() # Empty list object mylist[[ane]] <- x1 # Assign x1 instance vector to first list entry mylist[[2]] <- mtcars[one:3, 3:5] # Assign mtcars subset to second list entry

Every bit before, nosotros tin can apply is.nada to the whole listing:

                is                .                goose egg                (mylist)                # Check if list is NULL                # FALSE              

is.null(mylist) # Check if listing is Null # Simulated

Over again, after transforming the list to Zippo, the is.null role returns True:

mylist                <-                NULL                # Convert mylist to NULL                is                .                null                (mylist)                # Check if list is Zilch                # TRUE              

mylist <- NULL # Catechumen mylist to NULL is.null(mylist) # Check if list is NULL # Truthful

On a side note:
R provides several other is.xxx functions that are very similar to is.null (e.g. is.na, is.nan, or is.finite). All I've bear witness you lot here is applicative to many other R programming scenarios!

Video Explanation: Logical Values in R

The is.nothing function returns a logical vector. Do you need more than practice with logical vectors in R? And then I tin can recommend the following tutorial video of the DataCamp YouTube channel:

Further Reading

  • The is.na R Function
  • The R Programming Linguistic communication

Source: https://statisticsglobe.com/r-is-null-function/

0 Response to "How To Remove Null Values In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel