How to avoid NA columns in dcast() output?

后端 未结 4 1973
名媛妹妹
名媛妹妹 2021-01-21 05:43

How can I avoid NA columns in dcast() output from the reshape2 package?

In this dummy example the dcast() o

4条回答
  •  独厮守ぢ
    2021-01-21 06:26

    You could rename the NA column of the output and then make it NULL. (This works for me).

    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)) 
    
    setnames(x , c("setosa", "versicolor", "virginica", "newname"))
    
    x$newname <- NULL
    

提交回复
热议问题