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

+1 vote

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?

Apr 12, 2018 in Data Analytics by kappa3010
• 2,090 points
5,019 views

2 answers to this question.

0 votes

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 
  2

The second option is to convert it into a data.frame

as.data.frame(table(num))
       num Freq
1        2    1
2        7    1
3       13    2
4       16    1
...
answered Apr 12, 2018 by Sahiti
• 6,370 points
0 votes

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

answered Aug 21, 2019 by anonymous
• 33,030 points

Related Questions In Data Analytics

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,556 views
0 votes
1 answer

Counting the number of elements with the values of x in a vector

You may simply use the table() method: > ...READ MORE

answered Jun 14, 2022 in Data Analytics by Sohail
• 3,040 points
609 views
+1 vote
2 answers

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

answered Aug 14, 2019 in Data Analytics by anonymous
1,937 views
+1 vote
2 answers
+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,126 views
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
2,250 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,921 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by Gitika 1,182 views
0 votes
1 answer

How to get the output of number of elements to reach a cumulative sum?

You can use the sapply function, to loop ...READ MORE

answered May 29, 2018 in Data Analytics by Sahiti
• 6,370 points
795 views
0 votes
2 answers

How to remove rows with missing values (NAs) in a data frame?

Hi, The below code returns rows without ...READ MORE

answered Aug 20, 2019 in Data Analytics by anonymous
• 33,030 points
14,762 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