假设我有一个数据集data
:
x1 <- c("a","a","a","a","a","a","b","b","b","b")
x2 <- c("a1","a1","a1","a1","a1","a1","b1","b1","b2","b2")
data <- data.frame(x1,x2)
x1 x2
a a1
a a1
a a2
a a1
a a2
a a3
b b1
b b1
b b2
b b2
我想找到x1
对应的唯一值的数量x2
例如a
只有 3 个唯一值 ( a1,a2
and a3
) 并且b
有 2 个值 ( b1
and b2
)
我用过aggregate(x1~.,data,sum)
,但它不起作用,因为这些是因子,而不是整数。
请帮忙