I have a large data set which contains two columns.
This is a representation of it
l1=data.frame(c1=c("A","A","A","B","B","C","D","D"),c2=c("cat","dog","cow","pig","dog","horse","cat","goat"))
I need to create a frequency matrix with c1 as the columns and c2 as the rows and the number of occurrences of each value of c2 in c1 in each cell.
The output should look something like this
c1
c2 A B C D
cat 1 0 0 1
cow 1 0 0 0
dog 1 1 0 0
goat 0 0 0 1
horse 0 0 1 0
pig 0 1 0 0
I tried using table() and xtabs(). They work for this smaller dataset but not on my actual very large one. Also a solution without for loops would be helpful as it is a very large dataset. Thanks!