Table of Interactions - Case with pets and houses

北城以北 提交于 2019-11-26 02:13:12

Use table and crossprod:

out <- crossprod(table(houses, animals))
out[lower.tri(out, diag=TRUE)] <- NA
out
#         animals
# animals  cat dog rat snake spider
#   cat     NA   1   2     1      1
#   dog     NA  NA   0     0      0
#   rat     NA  NA  NA     1      1
#   snake   NA  NA  NA    NA      1
#   spider  NA  NA  NA    NA     NA

Since the output is a matrix you can suppress the printing of the NA values directly in print:

print(out,na.print="")
#         animals
# animals  cat dog rat snake spider
#   cat          1   2     1      1
#   dog              0     0      0
#   rat                    1      1
#   snake                         1
#   spider                         
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!