When using dcast with the subset argument, I get the following error when the dcast on the original data frame & the dcast on the subset data frame do not match in rows.
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 2, 3
I have reproduced the error with the mtcars dataset. Below is the code with the reproduction.
library(reshape2)
# dataframe
mtcars2 <- mtcars[, c('vs','am','gear','carb')]
mtcars2$cars <- row.names(mtcars)
row.names(mtcars2) <- NULL
mtcars2$dummyvariable <- 1
mtcars2.melt <- melt(mtcars2, id=c('cars','vs','am','gear','carb'))
colnames(mtcars2.melt)
# [1] "cars" "vs" "am" "gear" "carb" "variable" "value"
dcast(mtcars2.melt, vs ~ am, drop=FALSE, margins=TRUE)
# Aggregation function missing: defaulting to length
# vs 0 1 (all)
# 1 0 12 6 18
# 2 1 7 7 14
# 3 (all) 19 13 32
cadillac <- subset(mtcars2.melt, regexpr('Cadillac',cars)>0)
dcast(cadillac, vs ~ am, drop=FALSE, margins=TRUE)
# Error in data.frame(..., check.names = FALSE) :
# arguments imply differing number of rows: 2, 3
dcast(cadillac, vs ~ am, margins=TRUE)
# vs 0 (all)
# 1 0 1 1
# 2 (all) 1 1
The last dcast shows that the error can be avoided by skipping the drop=FALSE condition, but my desired output is
vs 0 1 (all)
1 0 1 0 1
2 1 0 0 0
3 (all) 1 0 1
Any help would be great! :)
Thanks
sessionInfo()
? This code works fine for me – hadleycode
dcast(mtcars2.melt, vs ~ am, drop=FALSE, margins=TRUE, subset=.(regexpr('Cadillac',cars)>0)) Using dummyvariable as value column: use value_var to override. Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 2, 3code
– user662963code
R version 2.12.1 (2010-12-16) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] plyr_1.4 reshape2_1.1 loaded via a namespace (and not attached): [1] stringr_0.4 – user662963