How can I avoid NA
columns in dcast()
output from the reshape2
package?
In this dummy example the dcast()
output will include an NA
column:
require(reshape2)
data(iris)
iris[ , "Species2"] <- iris[ , "Species"]
iris[ 2:7, "Species2"] <- NA
(x <- dcast(iris, Species ~ Species2, value.var = "Sepal.Width",
fun.aggregate = length))
## Species setosa versicolor virginica NA
##1 setosa 44 0 0 6
##2 versicolor 0 50 0 0
##3 virginica 0 0 50 0
For a somewhat similar usecase, table()
does have an option that allows to avoid this:
table(iris[ , c(5,6)], useNA = "ifany") ##same output as from dcast()
## Species2
##Species setosa versicolor virginica <NA>
## setosa 44 0 0 6
## versicolor 0 50 0 0
## virginica 0 0 50 0
table(iris[ , c(5,6)], useNA = "no") ##avoid NA columns
## Species2
##Species setosa versicolor virginica
## setosa 44 0 0
## versicolor 0 50 0
## virginica 0 0 50
Does dcast()
have a similar option that removes NA
columns in the output? How can I avoid getting NA
columns? (This function has a number of rather cryptic options that are sternly documented and that I cannot quite grasp...)