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

0 votes

I have a vector of numbers:

numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,
         453,435,324,34,456,56,567,65,34,435)

How can I have R count the number of times a value x appears in the vector?

Jun 14 in Data Analytics by Avinash
• 1,260 points
26 views

1 answer to this question.

0 votes
You may simply use the table() method:

> a <- table(numbers) > a \snumbers
4 5 23 34 43 54 56 65 67 324 435 453 456 567 657 324 435 453 456 567 657 324 435 453 456 567 657 324 435 4
2 1 2 1 2 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Then you can subset it:

> a[names(a)==435]
435
3
Or convert it into a data.frame if you're more comfortable working with that:

> as.data.frame(table(numbers)) numbers Freq 1 4 2 2 5 1 3 23 2 4 34 2
answered Jun 14 by Sohail
• 2,960 points

Related Questions In Data Analytics

+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
982 views
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,720 points
3,227 views
+1 vote
2 answers
0 votes
1 answer
0 votes
1 answer

Test if a vector contains a given element

1. Making use of the std::count function Counting ...READ MORE

answered Jun 20 in Data Science by Sohail
• 2,960 points
21 views
0 votes
1 answer

How to create excellent examples in R?

An excellent example must consist of the ...READ MORE

answered Apr 10, 2018 in Data Analytics by kappa3010
• 2,090 points

edited Apr 12, 2018 by kappa3010 249 views
+1 vote
2 answers

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

Use dplyr function group_by(). > n = as.data.frame(num) > ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,010 points
3,448 views
0 votes
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,740 points
155,381 views
0 votes
1 answer

How to use the strsplit function with a period

Since the dot has a specific meaning ...READ MORE

answered Jun 24 in Data Analytics by Sohail
• 2,960 points
18 views
0 votes
1 answer

How can I make a list of lists in R?

Lists of lists are simple to create. list1 ...READ MORE

answered Jun 24 in Data Analytics by Sohail
• 2,960 points
14 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP