I want to know the number of unique values, number of 2, 3 or more values
x
name var
1 a abc1
2 b abc1
3 c abc2
4 d abc2
5 e abc2
6 f accd1
7 g baac1
y <- data.frame(table(x$var))
y
Var1 Freq
1 abc1 2
2 abc2 3
3 accd1 1
4 baac1 1
if using table()
, I can see how many of them Var1
have. BUT, What I want to know is.. how many unique value's got. (and 2, 3 or more)
like this.
unique value : 2 #(accd1, baac1)
2 value : 1 #(abc1)
3 or more : 1 #(abc2)
using tidyverse
functions, you can try something like
df %>%
group_by(var) %>%
mutate( cnt = n()) %>%
ungroup() %>%
group_by(cnt) %>%
mutate(freq = n()) %>%
select(cnt, freq) %>%
unique()
You can do :
freq <- data.frame(table(x$var))$Freq
table(ifelse(freq >= 3, "3 and more", freq))
> table(ifelse(freq >= 3, "3 and more", freq))
1 2 3 and more
2 1 1
Or with the dplyr
library :
#install.packages("dplyr")
library(dplyr)
x %>%
count(var) %>%
mutate(values = ifelse(n >= 3, "3 and more", n)) %>%
count(values)
# A tibble: 3 x 2
values nn
<chr> <int>
1 1 2
2 2 1
3 3 and more 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.