Method for Counting TRUE Values ​​in a Logical Vector | r-bloggers

- Advertisement -


[This article was first published on Data Analysis in R, and kindly contributed to R-bloggers], (You can report a problem about the content of this page here) Want to share your content on R-Blogger? Click here if you have a blog, or click here if you don’t.

- Advertisement -

The POST method for calculating TRUE values ​​in a logical vector first appeared on finnstats.

- Advertisement -

If you are interested in learning more about data science, you can find more articles on finstats here.

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:

- Advertisement -

Method 1: Use sum()

Method 2: Use summary()

The following examples demonstrate each technique in action.

Highest Paying Data Science Skills – You Should Know! ,

Example 1: 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:

Now we can create a logical vector.

x <- c (true, false, false, true, false, false, not applicable, not applicable)

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 the correct values ​​using summary()

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

Now we can develop a logical vector.

x <- c (no, false, false, true, false, false, not applicable, true)

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

Logical 4 2 2 of Summary (x) Mode FALSE TRUE NA

The output reveals the following:

The vector has 4 FALSE values.

The vector has two TRUE values.

The vector contains two NA values.

If you want to know how many times each type of value appears in a logical vector, the summary() function is very helpful.

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

Let’s count the TRUE value in the vector

Summary (x)[‘TRUE’]
true 2

Introduction to Hadoop Data Processing Applications »

If you are interested in learning more about data science, you can find more articles on finstats here.

The POST method for calculating TRUE values ​​in a logical vector first appeared on finnstats.

related



Source link

- Advertisement -

Recent Articles

Related Stories