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,130 points
1,719 views

2 answers 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,170 points
0 votes
You can try this way,

as.data.frame(v) %>% count(v)
answered Aug 8 by anonymous

Related Questions In Data Analytics

0 votes
4 answers

How to remove NA values from a Vector in R?

You can try na.omit() or na.exclude() too. ...READ MORE

answered Jul 31 in Data Analytics by anonymous
• 31,840 points
8,448 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,690 points
189 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
26 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
• 42,330 points
60 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
76 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
184 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
121 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
61 views
0 votes
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 in Data Analytics by anonymous
• 31,840 points
130 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,170 points
169 views