Table of Contents
To count the number of TRUE values in a logical vector, one can use the function “sum()” in R. This function will add up all the TRUE values in the vector and return the total count. Alternatively, one can also use the “length()” function to get the total number of elements in the vector and then use the “sum()” function on a logical expression to count the TRUE values.
R: Count TRUE Values in Logical Vector
You can use the following methods to count the number of TRUE values in a logical vector in R:
Method 1: Use sum()
sum(x, na.rm=TRUE)
This method will return the count of TRUE values in a vector.
Method 2: Use summary()
summary(x)
This method will return the count of TRUE, FALSE, and NA values in a vector.
The following examples show how to use each method in practice.
Example 1: Count TRUE Values Using sum()
The following code shows how to use sum() to count the number of TRUE values in a logical vector:
#create logical vector x <- c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, NA, TRUE) #count TRUE values in vector sum(x, na.rm=TRUE) [1] 3
From the output we can see that there are 3 TRUE values in the vector.
Note: If there are NA values in the vector and we don’t use the argument na.rm=TRUE, then the function will return NA.
Example 2: Count TRUE Values Using summary()
The following code shows how to use summary() to count the number of TRUE, FALSE, and NA values in a logical vector:
#create logical vector x <- c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, NA, TRUE) #count TRUE, FALSE, and NA values in vector summary(x) Mode FALSE TRUE NA's logical 4 3 1
From the output we can see:
- There are 4 FALSE values in the vector.
- There are 3 TRUE values in the vector.
- There is 1 NA value in the vector.
The summary() function is particularly useful if you’d like to know the occurrence of each type of value in a logical vector.
If you’d like to only return the number of TRUE values from the summary() function, you can use the following syntax:
#create logical vector x <- c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, NA, TRUE) #count TRUE values in vector summary(x)['TRUE'] TRUE 3
From the output we can see that there are 3 TRUE values in the vector.
Cite this article
stats writer (2024). How can I count the number of TRUE values in a logical vector?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-true-values-in-a-logical-vector/
stats writer. "How can I count the number of TRUE values in a logical vector?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-true-values-in-a-logical-vector/.
stats writer. "How can I count the number of TRUE values in a logical vector?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-true-values-in-a-logical-vector/.
stats writer (2024) 'How can I count the number of TRUE values in a logical vector?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-count-the-number-of-true-values-in-a-logical-vector/.
[1] stats writer, "How can I count the number of TRUE values in a logical vector?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I count the number of TRUE values in a logical vector?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
