 Apr 19, 2018 2,407 views

## 2 answers to this question.

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 19, 2018 by
• 3,690 points

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 20 by anonymous
• 31,840 points

## 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

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

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

## How to remove NA values from a Vector in R?

You can try na.omit() or na.exclude() too. ...READ MORE

## 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

## How can I find number of rows in a dataframe, and how to find the number of rows that dont have null values

Use nrow() to find number of rows ...READ MORE

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

You can write a custom sapply function ...READ MORE

## 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

+1 vote

## Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE) ...READ MORE