Suppose I have a data set 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
I want to find the number of unique values of x1 corresponding to x2
For example a has only 3 unique values (a1,a2 and a3) and b has 2 values (b1 and b2)
I used aggregate(x1~.,data,sum) but it did not work since these are factors, not integers.
Please help