The is.logical() function in R checks whether an input object’s data type is logical. If the object is logical and its values are either TRUE or FALSE, it returns TRUE; otherwise, it returns FALSE.
This function checks the underlying type of the object. Due to the nature of the function, you can use it in conditional statements to validate user input.
is.logical(obj)
Name | Value |
obj | It is an object that needs to be checked for a logical type. |
logical_val_one <- TRUE
logical_val_two <- FALSE
is.logical(logical_val_one) # TRUE
is.logical(logical_val_two) # TRUE
You can see that TRUE and FALSE are logical values and return TRUE.
Let’s pass non-logical values and see the output:
is.logical(0) # FALSE
is.logical("hello") # FALSE
0 is the logical gate, but in R, it is a numeric value, so it returns FALSE.
If you create a vector that contains TRUE or FALSE values, this function returns TRUE.
vec_logical <- c(TRUE, FALSE, TRUE, FALSE)
is.logical(vec_logical) # TRUE
If you pass a non-logical vector, it returns FALSE.
non_logical_vec <- c(1, 2, 3, 4)
is.logical(non_logical_vec) # FALSE
The lists are not logical objects, so even if you pass all the elements of the list as logical values, it still returns FALSE.
list_obj <- list(TRUE, FALSE)
is.logical(list_obj) # FALSE
NA represents a missing value, and a missing value can be anything, including a logical type.
So, if you pass NA to the is.logical() function, it returns TRUE.
is.logical(NA) # TRUE
NaN represents undefined numerical results, which means they are not logical values by default and cannot be converted to one. So, it returns FALSE.
is.logical(NaN) # FALSE
NULL represents an empty object, which means there is nothing, including logical values. Hence, it returns FALSE.
is.logical(NULL) # FALSE
That’s all!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
The scale() function in R centers (subtracting the mean) and/or scales (dividing by the standard…
To rename a file in R, you can use the file.rename() function. It renames a…
The prop.table() function in R calculates the proportion or relative frequency of values in a…
The exp() is a built-in function that calculates the exponential of its input, raising Euler's…
The split() function divides the input data into groups based on some criteria, typically specified…
The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…