· r-2

R/plyr: ddply - Error in vector(type, length) : vector: cannot make a vector of mode 'closure'.

In my continued playing around with plyr’s ddply function I was trying to group a data frame by one of its columns and return a count of the number of rows with specific values and ran into a strange (to me) error message.

I had a data frame:

n = c(2, 3, 5)
s = c("aa", "bb", "cc")
b = c(TRUE, FALSE, TRUE)
df = data.frame(n, s, b)

And wanted to group and count on column 'b' so I’d get back a count of 2 for TRUE and 1 for FALSE. I wrote this code:

ddply(df, "b", function(x) {
  countr <- length(x$n)
  data.frame(count = count)
})

which when evaluated gave the following error:

Error in vector(type, length) :
  vector: cannot make a vector of mode 'closure'.

It took me quite a while to realise that I’d just made a typo in assigned the count to a variable called 'countr' instead of 'count'.

As a result of that typo I think the R compiler was trying to find a variable called 'count' somwhere else in the lexical scope but was unable to. If I’d defined the variable 'count' outside the call to ddply function then my typo wouldn’t have resulted in an error but rather an unexpected resulte.g.

> count = 10
> ddply(df, "b", function(x) {
+   countr <- length(x$n)
+   data.frame(count = count)
+ })
      b count
1 FALSE     4
2  TRUE     4

Once I spotted the typo and fixed it things worked as expected:

> ddply(df, "b", function(x) {
+   count <- length(x$n)
+   data.frame(count = count)
+ })
      b count
1 FALSE     1
2  TRUE     2
  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket