Juan
Juan

Reputation: 422

R count how many times a value appears in a data frame

I have a data frame. All columns are numbers. How to count the times that a value appears in all the data frame? For example the number 9999.

Upvotes: 0

Views: 729

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145745

In R (and many programming languages) counting is done as a sum of a logical condition - TRUE is equivalent to 1, and FALSE is equivalent to 0. In this case, if your data is named df then df == 9999 gives TRUE when the data is 9999 and FALSE otherwise, and to count the number of TRUE values we use sum():

sum(df == 9999)

Upvotes: 2

Related Questions