1

Using each in plyr, the first two each commands work without error, but the third one produces the error

Error: Results must have the same dimensions.

The only difference in the three each commands is in the third argument ("mode"=...)

library(plyr)

each("mean"=function(x) ifelse(is.numeric(x),mean(x),NA)
   , "median"=function(x) ifelse(is.numeric(x), median(x), NA) 
   , "mode"=function(x) names(which.max(table(x))))

each("mean"=function(x) ifelse(is.numeric(x),mean(x),NA)
   , "median"=function(x) ifelse(is.numeric(x), median(x), NA)
   , "mode"=function(x) ifelse(TRUE, names(which.max(table(x))), NA))

each("mean"=function(x) ifelse(is.numeric(x),mean(x),NA)
   , "median"=function(x) ifelse(is.numeric(x), median(x), NA)
   , "mode"=function(x) ifelse(is.factor(x), names(which.max(table(x))), NA))

I fail to understand why.

matthiash
  • 3,105
  • 3
  • 23
  • 34
  • 1
    Could you make your example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? Without that we are left guessing as to what goes wrong. – Paul Hiemstra Feb 09 '17 at 09:33
  • @PaulHiemstra, I added library(plyr), now the example is reproducible – matthiash Feb 09 '17 at 09:42
  • It's very strange because if you put "TRUE" (the result that 'is.factor(x)' should return) instead of the last 'is.factor(x)' it works. – Andrea Ianni Feb 09 '17 at 10:38

0 Answers0