changed from model matrix form which did not work to use dplyr, which should be portable among functions