b587c08650429c13462c7a5b62a9dad0
2019.43
edureka.co
b587c08650429c13462c7a5b62a9dad0
consider a vector of numbers:num <- c(2,13,7,13,51,43,44,76,647,37,367,435,
 443,425,314,31,416,16,367,65,44,425)how can i count the number of times a value 'x' appears in the vector with r?
how to count the number of elements with the values in a vector?
edureka.co
2019.43
you have various options to count the number of times a number occurs:one of them is to use table():z <- table(num)
> z
num
 2 7 13 16 31 37 43 44 51 65 76 314 367 416 425 435 443 647 
 1 1 2 1 1 1 1 2 1 1 1
1 2 1 1 1 1 1 1
then you can subset it, for the number you want the count for:z[names(z)==367]
367 
 2the second option is to convert it into a data.frameas.data.frame(table(num))
 num freq
1 2 1
2 7 1
3 13 2
4 16 1
...
use dplyr function group\_by().> n = as.data.frame(num)
> n %>% group\_by(num) %>% count()     num     n
   <dbl> <int>
 1     2     1
 2     7     1
 3    13     2
 4    16     1
 5    31     1
 6    37     1
 7    43     1
 8    44     2
 9    51     1
10    65     1
11    76     1
12   314     1
13   367     2
14   416     1
15   425     2
16   435     1
17   443     1
18   647     1
