0

I'm trying to use ddply to process each row of a dataframe with a function as argument passed to ddply.

The following is a simplified illustration. But I got the error: Error in eval(expr, envir, enclos) : object 'x' not found

> df <- data.frame(standard = c("banana", "apple"), variants = c("xiangjiao", "pingguo"))
> 
> df
  standard  variants
1   banana xiangjiao
2    apple   pingguo
> ddply(df, .(x), function(x) { cat("Here is a row: ", x) })
Error in eval(expr, envir, enclos) : object 'x' not found
> 

I'm following the example found at For each row in an R dataframe

It looks so straightforward. I assumed the x would stand for the value of each row here.

library(plyr)
ddply(dataFrame, .(x), function(x) { # do stuff })

But it seems that there are still some nuance that I don't know.

Thanks for your help.

Yu

Community
  • 1
  • 1
Yu Shen
  • 2,770
  • 3
  • 33
  • 48
  • The problem isnt the function it is that you are telling ddply to group your data frame by a variable x that doesnt exists. – joran Apr 17 '15 at 00:53
  • The second argument should be the grouping variable, which could be `standard` or `variants`. But if you want to apply it to every row, you probably want to go with the approach [here](http://stackoverflow.com/questions/2074606/doing-a-plyr-operation-on-every-row-of-a-data-frame-in-r) – David Robinson Apr 17 '15 at 00:53
  • @joran, and David Robinson, Thanks for pointing out the root cause of the problem. Yes, you're right that I thought ddply could almost anything, but actually it essentially do aggregate and processing. – Yu Shen Apr 17 '15 at 02:14

0 Answers0