How can I avoid NA columns in dcast() output from the reshape2 package?
In this dummy example the dcast() o
One solution that I've found, which I'm not positively unhappy with, is based on the dropping NA values approach suggested in the comments. It leverages the subset argument in dcast() along with .() from plyr:
require(plyr)
(x <- dcast(iris, Species ~ Species2, value.var = "Sepal.Width",
fun.aggregate = length, subset = .(!is.na(Species2))))
## Species setosa versicolor virginica
##1 setosa 44 0 0
##2 versicolor 0 50 0
##3 virginica 0 0 50
For my particular purpose (within a custom function) the following works better:
(x <- dcast(iris, Species ~ Species2, value.var = "Sepal.Width",
fun.aggregate = length, subset = .(!is.na(get("Species2")))))
## Species setosa versicolor virginica
##1 setosa 44 0 0
##2 versicolor 0 50 0
##3 virginica 0 0 50