简体   繁体   中英

Finding the number of unique variables per factor in R

I have a dataframe which looks like this:

id <- c(1,2,3,4,5,6,7,8,9,10)
val <- c("a", "b", "c", "a", "b", "a", "c", "a", "a", "c")
df <- data.frame(id,val)

I am trying to create a vector of length 10 which, for every id, gives the number of rows in df with the same value val. The output should be

out <- c(5, 2, 3, 5, 2, 5, 3, 5, 5, 3)

It's basically the opposite of

with(df, tapply(val, id, function(x) length(unique(x))))

If that makes sense? Maybe I could merge with(df, tapply(id, val, function(x) length(unique(x)))) with df somehow, but that seems like a very ugly solution.

您可以这样做:

table(df$val)[df$val]

The ave function is meant for tasks such as this

cc<-with(df, ave(id,val, FUN=length))
cbind(df, cc)

will result in

id val cc
1   1   a  5
2   2   b  2
3   3   c  3
4   4   a  5
5   5   b  2
6   6   a  5
7   7   c  3
8   8   a  5
9   9   a  5
10 10   c  3

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM