如何避免dcast()输出中的NA列? [英] How to avoid NA columns in dcast() output?
问题描述
我如何避免 从reshape2
软件包输出的dcast()
列?
How can I avoid NA
columns in dcast()
output from the reshape2
package?
在此虚拟示例中,dcast()
输出将包含NA
列:
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
对于有些类似的用例,table()
确实有一个选项可以避免这种情况:
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
dcast()
是否具有类似的选项,可以删除输出中的NA
列?如何避免出现NA
列? (此功能有许多相当隐秘的选项,已严格记录在案,我无法完全理解...)
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...)
推荐答案
这是我如何解决它的方法:
Here is how I was able to get around it:
iris[is.na(iris)] <- 'None'
x <- dcast(iris, Species ~ Species2, value.var="Sepal.Width", fun.aggregate = length)
x$None <- NULL
这个想法是用"None"替换所有NA,以便dcast创建一个名为"None"而不是"NA"的列.然后,如果不需要,您可以在下一步中将该列删除.
The idea is that you replace all the NAs with 'None', so that dcast creates a column called 'None' rather than 'NA'. Then, you can just delete that column in the next step if you don't need it.
这篇关于如何避免dcast()输出中的NA列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!