Suppose my data seems the following. Columns v1-v3
and w1-w3
are the dummies of the variables named V
and W
, respectively.
v1 v2 v3 w1 w2 w3
1 0 0 0 1 0
0 1 0 0 0 1
0 0 1 1 0 0
1 0 0 0 1 0
0 1 0 0 0 1
0 0 1 1 0 0
My intention is to produce a frequency table considering v1, v2, v3
as rows & w1, w2, w3
as columns. Any help is highly appreciated!
I am afraid I can only do it with loops. There might well be a more elegant way to do this in R but at least it works.
I don't know what you want to put in the cells when using v1-3 as rows and w1-3 as columns. You might put the times the values were the same or the times the values were 1. Or any other formula. In the example below, I tried two different ways (one is commented out)
dta = data.frame(v1=c(1,0,0,1,0,0),
v2=c(0,1,0,0,1,0),
v3=c(0,0,1,0,0,1),
w1=c(0,0,1,0,0,1),
w2=c(1,0,0,1,0,0),
w3=c(0,1,0,0,1,0))
t = matrix(NA,nrow=3,ncol=3)
colnames(t)=names(dta[4:6])
rownames(t)=names(dta[1:3])
for(r in rownames(t)){
for(c in colnames(t)){
t[r,c]=sum(dta[[r]]==dta[[c]]) ## Agreement
#t[r,c]=sum(dta[[r]]==1 & dta[[c]]==1) ## Both are 1
}
}
The script first creates the table and then loops through all cells to fill them in with the correct value, taking information from the original dataset.
For agreement, this matrix would result:
> print(t)
w1 w2 w3
v1 2 6 2
v2 2 2 6
v3 6 2 2
This means that, for example, v1 and w2 agree in 6 cases, while the agreement of v1 with w1 is only 2.