# Method for Counting TRUE Values in a Logical Vector

**Data Analysis in R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Method for Counting TRUE Values in a Logical Vector appeared first on finnstats.

If you are interested to learn more about data science, you can find more articles here finnstats.

Method for Counting TRUE Values in a Logical Vector, The following techniques can be used to determine how many TRUE values are present in a logical vector in R:

**Method 1: Use sum()**

**Method 2: Use summary()**

The examples that follow demonstrate each technique in action.

Highest Paying Data Science Skills-You should know! »

## Example1: Count TRUE Values with sum ()

The number of TRUE values in a logical vector can be counted using sum() as seen in the code below:

We can now construct a logical vector.

x <- c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, NA, NA)

Count the TRUE values in the vector.

sum(x, na.rm=TRUE) [1] 2

We can see from the result that the vector has 2 TRUE values.

The function will return NA if the vector contains NA values and the na.rm=TRUE argument is not used.

Not Satisfied with statistical significance (p-value) »

## Example 2: Count TRUE Values Using summary()

How to use summary() to count the number of TRUE, FALSE, and NA values in a logical vector is demonstrated in the code below:

We can now develop a logical vector.

x <- c(NA, FALSE, FALSE, TRUE, FALSE, FALSE, NA, TRUE)

Now we can create a count of TRUE, FALSE, and NA values in the vector

summary(x) Mode FALSE TRUE NA's logical 4 2 2

The output reveals the following:

The vector contains 4 FALSE values.

The vector has two TRUE values.

The vector has two NA values.

If you want to know how often each sort of value appears in a logical vector, the summary() function is quite helpful.

The following syntax can be used to just return the number of TRUE values from the summary() function:

Let’s count TRUE values in the vector

summary(x)['TRUE'] TRUE 2

Introduction to Hadoop Data Processing Applications »

If you are interested to learn more about data science, you can find more articles here finnstats.

The post Method for Counting TRUE Values in a Logical Vector appeared first on finnstats.

**leave a comment**for the author, please follow the link and comment on their blog:

**Data Analysis in R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.