Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting number of times a value occurs

Tags:

r

I have a variable (Var) which stores 10000 values and is of integer nature.

I want to count, how many times 1000 or higher than 1000 numeric value occurs in this list.

Any one liner in R?

Thank you in advance.

like image 302
Angelo Avatar asked Aug 23 '11 14:08

Angelo


People also ask

How do you count occurrences?

You can use the =UNIQUE() and =COUNTIF() functions to count the number of occurrences of different values in a column in Excel.

How do you count the number of times a word occurs in Excel?

Select a cell next to the list you want to count the occurrence of a word, and then type this formula =COUNTIF(A2:A12,"Judy") into it, then press Enter, and you can get the number of appearances of this word.


1 Answers

sum(Var >= 1000) will do it, as long as Var is a vector. If not, you'll need to tell R to find Var inside whatever object contains it. Here is an example:

> set.seed(2)
> Var <- sample(900:1100, 10)
> Var
 [1]  937 1040 1014  933 1085 1084  925 1061  990 1005
> Var >= 1000
 [1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
> sum(Var >= 1000)
[1] 6

This uses the fact that TRUE = 1 and FALSE = 0.

like image 118
Gavin Simpson Avatar answered Sep 22 '22 09:09

Gavin Simpson