How to count unique values in R?

0 votes

Let's say I have:

v = rep(c(1,2, 2, 2), 25)

Now, I want to count the number of times each unique value appears.

the unique() gives the values which are unique but does not the number of unique values

> unique(v)
[1] 1 2

I want something that gives me

length(v[v==1])
[1] 25
length(v[v==2])
[1] 75

How do I achieve this?

Apr 9, 2018 in Data Analytics by DataKing99
• 8,100 points
934 views

1 answer to this question.

0 votes

You can get the information printed in a table as follows:

Data = rep(c(1,2, 2, 2), 25)

table(Data)
# Data
#  1  2 
# 25 75

## or another presentation of the same data
as.data.frame(table(Data))
#  Data      Freq
#  1         1   25
#  2         2   75
answered Apr 9, 2018 by darklord
• 6,140 points

Related Questions In Data Analytics

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,593 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,680 points
93 views
0 votes
0 answers

How to normalize values in fields to show in plots in R?

Hi, I use a dataset called airquality ...READ MORE

Jul 9 in Data Analytics by sindhu
15 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 37,320 points
37 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

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

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

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

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

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

Plotting multiple graphs on the same page in R

If you want to plot 4 graphs ...READ MORE

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

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

You have various options to count the ...READ MORE

answered Apr 12, 2018 in Data Analytics by darklord
• 6,140 points
68 views
0 votes
1 answer

How to change y axis max in time series using R?

The axis limits are being set using ...READ MORE

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