How can we count TRUE values in a logical vector

+1 vote
Apr 20, 2018 in Data Analytics by shams
• 3,670 points
10,122 views

2 answers to this question.

0 votes
In the cases of problems where logical vector contains NA values.

a <- c(TRUE, FALSE, NA)
sum(a) # gives you NA
table(a)["TRUE"] # gives you 1
length(a[a==TRUE]) # f3lix answer, gives you 2 (because NA indexing returns values)

sum(a, na.rm=TRUE) # best way to count TRUE values #which gives 1.

You should be careful with the "table" solution, in case there are no TRUE values in the logical vector.
Suppose a <- c(NA, FALSE, NA) or simply a <- c(FALSE, FALSE)

table(a)["TRUE"] # gives you NA for both cases.
answered Apr 20, 2018 by zombie
• 3,790 points
0 votes

Hi,

You can get a count of all values in a vector using table().

If you don't want to include NA, "", specific values in the count, then specify in exclude parameter.

To see count of rows try as below.

syntax: table(vector,exclude = c(values that you want to exclude),useNA = "no")

table(vector,exclude = c("False"),useNA = "no")
answered Aug 21, 2019 by anonymous
• 33,030 points

Related Questions In Data Analytics

+1 vote
2 answers

How to count the number of elements with the values in a vector?

Use dplyr function group_by(). > n = as.data.frame(num) > ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
5,050 views
0 votes
1 answer

How can I delete multiple values from a vector in R?

The %in% operator tells  which elements are ...READ MORE

answered Apr 27, 2018 in Data Analytics by shams
• 3,670 points
6,204 views
+1 vote
5 answers

How to remove NA values from a Vector in R?

Hello team, you can use na.omit x <- c(NA, 3, ...READ MORE

answered Dec 9, 2020 in Data Analytics by anonymous
• 82,840 points
195,386 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
4,592 views
+1 vote
2 answers
0 votes
2 answers

How can you find total number of null values in a dataset column wise?

data['Column Name'].isnull().sum() READ MORE

answered May 7, 2020 in Data Analytics by anonymous
2,568 views
0 votes
1 answer

How to replace only set of values in a vector to different values in a dataframe?

If you have a condition using which ...READ MORE

answered Aug 19, 2019 in Data Analytics by anonymous
• 33,030 points
788 views
0 votes
0 answers

How can I avoid the transformation into an atomic vector in a matrix (r programming)

I'm scrambling to come up with a ...READ MORE

Jul 5, 2022 in Data Analytics by avinash
• 1,840 points
464 views
+1 vote
2 answers

How can I drop columns by name in a data frame ?

We can Drop Columns by name in ...READ MORE

answered Apr 14, 2018 in Data Analytics by zombie
• 3,790 points
28,465 views
0 votes
1 answer

How can we trim leading and trailing whitespaces in R?

trimws {base} //Remove Leading/Trailing Whitespace Removes leading and/or ...READ MORE

answered Apr 18, 2018 in Data Analytics by zombie
• 3,790 points
2,039 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP