foo This snippet is failed with error information "attempt to apply non-function".Actually, data$substitute(attr) failed to obtain the attr attribute of lst.

You are watching: R attempt to apply non-function

The concrete example is as following:Let us create a list named lst.

lst Then lst$substitute(CLICK_DT) failed to obtain the CLICK_DT attribute of lst.

foo(lst, CLICK) should transform lst$CLICK into factor mode and foo(lst,CLICK_DT) should transform lst$CLICK_DT into factor mode.

What is the easy way to do this? Thanks.

Improve this question
edited Aug 5 "15 at 7:53
asked Aug 5 "15 at 7:23

7511 gold badge22 silver badges1010 bronze badges
| Show 1 more comment

2 Answers 2

Active Oldest Votes
In your code, data and attr are two different variables being passed into the function - attr is not an attribute (i.e. a column) in data. So, instead just do this:

foo ...and you"re done (in fact, if this is all your function will be doing, you do not even need to pass in that data variable, and using just function(attr) will work just fine).

Then, if attr is supposed to be a column in your data frame, then use the $ sign when passing a variable to the function thus:

# declare the functionfoo Hope this helps!

Improve this answer
edited Aug 5 "15 at 7:43
answered Aug 5 "15 at 7:38

Deolu ADeolu A
65411 gold badge55 silver badges1111 bronze badges
Add a comment |
I see you"re trying to apply non-standard argument evaluation using substitute() to allow specifying the target list component using an unquoted symbol as the second argument of foo(). Here"s how to do that:

foo Couple of additional points: You"re misusing the word "attribute". In R, "attribute" is a well-defined term that refers to a secondary data object that is attached to another (primary) data object. You can read/write the attributes of an object using attr() and attributes(). The word you should be using here is "component", since we"re talking about list components. You can also use the word "element", since lists are viewed as a kind of vector, and are even referred to as "generic vectors" in the R source.

Secondly, the dollar operator is actually a function. When you use substitute() to access the parse tree of an expression, if the expression involves the dollar operator, then the type of the return value will be "language" (with class "call" or "{" if surrounded by a braced block), rather than "symbol" (class "name"). This is also true of normal function calls, for example typeof(substitute(sum(1,2))) also returns type "language" (and also has class "call").

See more: Why Does Firefox Open So Many Processes ? Access Denied

There are different ways your requirement could be implemented. You could support a literal string, or a symbol (as I showed), or even apply some kind of sophisticated analysis of the parse tree of the argument, such as accepting a dollar call and extracting from the resulting parse tree the RHS, and using that symbol as the component name. You could even through type detection of the argument"s parse tree support some or all of these interfaces with a single function definition, although that would add complexity. I don"t think that would be worth it; the way I wrote foo() above I think is the best design, since the implementation is simple, but it also provides maximum convenience for the caller, since they can specify the target list component using an unquoted symbol.