我有一个与此类似的数据集:
x <- sample(c("A", "B", "C", "D", "E"), 50, replace=TRUE, prob=c(0.1, 0.2, 0.4, 0.25, 0.05))
y <- sample(c("xyz", "mno", "abc", "def", "hkl", "opq", "rst", "ghi", "fgh", "vwx"), 50, replace=TRUE, prob=c(0.2, 0.1, 0.07, 0.03, 0.1, 0.05, 0.05, 0.1, 0.15, 0.15))
d <- data.frame(x,y)
'data.frame': 50 obs. of 2 variables:
$ x: Factor w/ 5 levels "A","B","C","D",..: 4 1 4 2 3 4 1 5 4 2 ...
$ y: Factor w/ 10 levels "abc","def","fgh",..: 5 1 5 4 4 9 5 9 10 6 ...
table(d)
y
x abc def fgh ghi hkl mno opq rst vwx xyz
A 1 0 1 1 2 0 0 0 0 3
B 0 1 1 1 0 1 0 0 3 3
C 1 0 1 5 0 3 0 1 2 2
D 1 1 0 1 3 0 1 0 4 2
E 0 0 0 0 2 0 1 0 1 0
现在我想找出x中其中一个y中有多少y的百分比。像这样的东西
A B C D E
A 1 .3 .4 .4 .1
B .3 1 .5 .4 .1
C .4 .5 1 .4 .1
D .4 .4 .4 1 .3
E .1 .1 .1 .3 1
或者
A B C D E
A 10 3 4 4 1
B 3 10 5 4 1
C 4 5 10 4 1
D 4 4 4 10 3
E 1 1 1 3 10
你知道有什么办法吗?