I have a large data frame with 31 Columns (V1:V31) and a large number of rows that have different combinations of V1:V31. The NAs indicate when that variable was not part of the combination. The example below is a subset of my data with just the first 8 variables and first 5 combinations. I have ranked the value of each variable into low/mid/high.
V1 V2 V3 V4 V5 V6 V7 V8
1 Low Low High <NA> <NA> <NA> <NA> <NA>
2 Low Low <NA> High <NA> <NA> <NA> <NA>
3 Low Low <NA> <NA> High <NA> <NA> <NA>
4 Low Low <NA> <NA> <NA> <NA> High <NA>
5 Low Low <NA> <NA> <NA> <NA> <NA> High
What Im trying to find are the number of times a variable with one value (low/mid/high) is associated with another variable's value (low/mid/high).
For Example:
How often is V2 low/mid/ high when V1 is low?
How often is V2 low/mid/ high when V1 is mid?
How often is V2 low/mid/ high when V1 is high?
How often is V3 low/mid/ high when V2 is low?
And so on.
Expected Results:
V1low <- data.frame()
Low Mid High
V2 11 3 4
V3 45 5 34
V4 3 67 5
V5 6 45 5
V1mid<- data.frame()
Low Mid High
V2 11 3 4
V3 45 5 34
V4 3 67 5
V5 6 45 5
V1high<- data.frame()
Low Mid High
V2 11 3 4
V3 45 5 34
V4 3 67 5
V5 6 45 5
V2low<- data.frame()
Low Mid High
V3 11 3 4
V4 45 5 34
V5 3 67 5
V6 6 45 5
How can I go about this problem?
Edit using @dario 's solution:
> results <- table(Pvart2$V1, Pvart2$V2, Pvart2$V3, Pvart2$V4, useNA="always",deparse.level = 2)
> results
, , Pvart2$V3 = High, Pvart2$V4 = High
Pvart2$V2
Pvart2$V1 High Low Med <NA>
High 0 0 0 0
Low 0 0 0 0
Med 0 0 0 0
<NA> 0 0 0 2
At higher dimensions than two the conditions for each dataframe become too high, resulting in too many results. The above example looks for values of V1 and V2 when V3 is high and V4 is high. But that condition is never fulfilled in the 2700 rows of combinations I have. The pairwise aspect also produces a lot of data that I cant look at quickly. Something like the expected results would be more manageable where for every Low V1, it tells me the how many low/mid/high per Variable V1 is associated with.