1
votes

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

1
Could you provide a copy of your sessionInfo()? This code works fine for mehadley
@hadley Sorry, the code that was causing the error included subset in the dcast code 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, 3 codeuser662963
That code works fine for me too.hadley
@hadley I have tried the above dcast with subset for this mtcars2 dataset multiple times with the same result. Not sure what I am doing differently. My sessionInfo looks like this code 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.4user662963

1 Answers

0
votes

Interesting problem! I've attempted this in the past and couldn't solve it. Basically I was trying to use dcast to export a series of data frames (to csv) that no matter how they were subsetted they would have the same dimensions. This would then allow me to cleanly 'join' them together in Excel or Powerpoint.

trying the new dcast still gives me an error after running the edited code above.

> dcast(mtcars2.melt, vs ~ am, drop=FALSE, margins=TRUE, subset=.(regexpr('Cadillac',cars)>0))
Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 2, 3

and looking at my Session

> sessionInfo()
R version 2.12.2 (2011-02-25)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C

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  tools_2.12.2

----

The error occurs when using DROP=F and MARGINS=T. And the specific cause of the problem appears to be when trying to cbind(res$labels[[1]], data) inside the dcast. Adding some print statements inside of dcast shows what is going on:

print("printing data")
print(data)
print("printing res$labels[[1]]")
print(res$labels[[1]])
print("trying cbind(res$labels[[1]], data)")


[1] "printing data"
   0 (all) NA
1  1    NA  1
2 NA    NA NA
3  1    NA  1
[1] "printing res$labels[[1]]"
     vs
1     0
2 (all)
[1] "trying cbind(res$labels[[1]], data)"
Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 2, 3