First of all let's put together a factor
that has the elements of your vector x
and whose levels are the complete universe of event A:N
x <- factor(c("J", "J", "K", "K", "K", "M", "J", "J", "J", "M"),
levels = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")
)
Now calculating the individual probability of each event of x
is simple with table()
:
valores <- table(x)/length(x)
valores
x
A B C D E F G H I J K L M N
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.3 0.0 0.2 0.0
But what you are looking for is the probability of obtaining any combination of two events so we should generate the 14x14 combinations and calculate the probability of each of these:
# Producto cartesiano para generar todas las combinaciones
# de dos eventos
final <- merge(valores, valores, by=NULL)
# Calculo la probabilidad de cada combinación
# Y armamos una columna para mostrar la misma
final$ProbXY <- final$Freq.x * final$Freq.y
final$Combinacion <- paste0(final$x.x, " - ", final$x.y)
final <- final[,c("Combinacion", "ProbXY")]
# Mostramos solo las combinaciones cuya probabilidad > 0
final[final$ProbXY != 0, ]
Combinacion ProbXY
136 J - J 0.25
137 K - J 0.15
139 M - J 0.10
150 J - K 0.15
151 K - K 0.09
153 M - K 0.06
178 J - M 0.10
179 K - M 0.06
181 M - M 0.04