Skip to content

Change of parsing parameters to functions between 0.63.1 and 0.63.3 ?

4 messages · Peter Dalgaard, Brian Ripley, Athula Herath

#
Hi,


I wonder whether the mechanism of parsing parameters to functions has
changed between 0.63.1 and 0.63.3? The following code yeils different
results in R 0.63.1 (Version 0.63.1  (Dec 5, 1998)) and R 0.63.3. 

cave<-function(x,a,b)
{
	return(c(mean(x[a],na.rm=T),mean(x[b],na.rm=T)))
}
datx <- data.frame(rbind(c(1,2,3,4),c(4,5,6,7)))
names(datx)<-c("A","B","C","D")
f1<-c("A","C")
f2<-c("B","D")
t1<-apply(datx,1,cave,f1,f2)




R 0.63.1 Result:
1 2
[1,] 2 5
[2,] 3 6


R 0.63.3 Result:
1   2
[1,] NaN NaN
[2,] NaN NaN


If I include a 

cat("x[a]->",a,"x[b]->",b,"\n")

in the function (cave)

R 0.63.1 shows the

x[a]-> 1 3 x[b]-> 2 4
x[a]-> 4 6 x[b]-> 5 7

Whereas in R 0.63.3 the result is :

x[a]-> NA NA x[b]-> NA NA 
x[a]-> NA NA x[b]-> NA NA 

Obviously, something is not right. I have verified this in both Unix
(Solaris, Linux) and also on WinNT

However, R Version

Version 0.64.0 Unstable (February 4, 1999)

Reports a similar behaviour to Version 0.63.1 : i.e. giving me the
correct results. 

Many Thanks,


Athula
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Athula Herath <Athula.Herath at ogs.co.uk> writes:
Yep. Error was that element names were not passed on to the FUN in
apply() in the vector case. The following patch (against 0.63.x
snapshot or 0.64 unstable) seems to fix it:
 

*** /tmp/T0a0058J	Thu Mar 25 05:51:46 1999
--- apply.R	Thu Mar 25 05:51:46 1999
***************
*** 29,35 ****
  
      ans <- vector("list", d2)
      if((i.vec <- length(d.call) < 2)) # vector
! 	for(i in 1:d2) ans[[i]] <- FUN(newX[,i], ...)
      else
  	for(i in 1:d2) ans[[i]] <- FUN(array(newX[,i], d.call, dn.call), ...)
  
--- 29,39 ----
  
      ans <- vector("list", d2)
      if((i.vec <- length(d.call) < 2)) # vector
! 	for(i in 1:d2){
! 	    xi<-newX[,i]
! 	    names(xi)<-dn.call[[1]]
! 	    ans[[i]] <- FUN(xi, ...)
! 	}
      else
  	for(i in 1:d2) ans[[i]] <- FUN(array(newX[,i], d.call, dn.call), ...)
#
On Wed, 24 Mar 1999, Athula Herath wrote:

            
No, but the handling of attributes has.
The help page for apply says

Arguments:

       x: the array to be used.

and your x is not an array, although R does an as.matrix for you. The
problem is that you are expecting apply to hand you a row vector labelled
by the column names of the (coerced) matrix. This is true in some versions
of R but not in others, and AFAIK is not documented to be expected. The
names of a vector are attributes, and we have been debating which
attributes should be retained on subsetting (as here).
Yes, but that is an old snapshot: it does not do so on the March 24, 1999
version. (What is `correct' is debatable: I would suggest that your code is
incorrect if (as I think) it relies on undocumented features. (But then
most R code would be classified as incorrect!)
#
Many thanks for Prof Ripley and also Peter Dalgaard  for offering help
in this issue. I have
now changed the code to do the right thing. The function has been
changed to:


cave<-function(x,a,b,bb)
{
    names(x)<-bb
    cat("x[a]->",x[a],"x[b]->",x[b],"\n")
 
    return(c(mean(x[a],na.rm=T),mean(x[b],na.rm=T)))
}
datx <- data.frame(rbind(c(1,2,3,4),c(4,5,6,7)))
names(datx)<-c("A","B","C","D")
f1<-c("A","C")
f2<-c("B","D")
t1<-apply(datx,1,cave,f1,f2,names(datx))

It works fine.

Thanks again for your kind help.


Athula./
Prof Brian D Ripley wrote:
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._