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

0 votes

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,010 points
47 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 darklord
• 6,140 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,680 points
64 views
+1 vote
1 answer

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

You have missed out on "na.rm=TRUE" inside ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
38 views
0 votes
1 answer
0 votes
1 answer

With the help of tidyverse: how to rename a column to a variable name

With the help of Dplyr: rename function ...READ MORE

answered Apr 3, 2018 in Data Analytics by DeepCoder786
• 1,700 points

edited Apr 3, 2018 by DeepCoder786 25 views
0 votes
3 answers

How to remove NA values from a Vector in R?

using the function "complete.cases" for example, if you ...READ MORE

answered Jun 18 in Data Analytics by anonymous
4,136 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,700 points
25 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,700 points
68 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,700 points
39 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 28, 2018 in Data Analytics by darklord
• 6,140 points
31 views
0 votes
1 answer

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

You can use complete.cases in the following ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
3,401 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.