s-news
[Top] [All Lists]

Use of do.call and order

To: s-news@lists.biostat.wustl.edu
Subject: Use of do.call and order
From: "David Kane - GEODE <David Kane" <a296180@mica.fmr.com>
Date: Mon, 23 Apr 2001 08:17:35 -0400 (EDT)
Reply-to: a296180@mica.fmr.com
Hi,

When using do.call with order, I see some behavior I don't understand.

> version
Version 6.0 Release 1 for Sun SPARC, SunOS 5.6 : 2000 
> df <- data.frame(w = c(0,0,0,0,0), x = c(2,2,1,1,1), y = c(6,7,6,7,6))
> sapply(df, class)
         w         x         y 
 "integer" "integer" "integer"
> df
> df
  w x y 
1 0 2 6
2 0 2 7
3 0 1 6
4 0 1 7
5 0 1 6
> by.var = "x"
> do.call("order", df[by.var])
[1] 3 4 5 1 2
> by.var = c("x","y")
> do.call("order", df[by.var])
[1] 3 5 4 1 2

So far, so good. I use this structure because "by.var" will generally be an
argument to the larger function. However:

> by.var = "w"
> do.call("order", df[by.var])
Problem in sort.list: argument w= not matched: sort.list(w = c(0, 0, 0, 0, 0), 
na.last = na.last) 
Use traceback() to see the call stack
> traceback()
7: eval(action, sys.parent())
6: doErrorAction("Problem in sort.list: argument w= not matched: sort.list(w = 
c(0, 0, 0, 0, 0), na.last = na.last)", 1000)
5: sort.list(w = c(0, 0, 0, 0, 0), na.last = na.last)
4: order(w = c(0, 0, 0, 0, 0))
3: eval(this.call, local = sys.parent(1.))
2: do.call("order", df[by.var])
1: 
Message: Problem in sort.list: argument w= not matched: sort.list(w = c(0, 0, 
0, 0, 0), na.last = na.last) 
> 

Why should this fail in this case? Note that "order" itself seems to work fine,
as it should, with a vector of identical values:

> order(df[[by.var]])
[1] 1 2 3 4 5

Thanks,

Dave Kane

<Prev in Thread] Current Thread [Next in Thread>
  • Use of do.call and order, David Kane - GEODE <David Kane <=