Re: [Rd] matrices with names

2017-07-27 Thread Suzen, Mehmet
Not always, see what happens with lapply:
> x<-matrix(12,1,1)
> names(x)<-"one"
> y<-matrix(1,1,1)
> names(y)<-"one"
> dput(lapply(x,`+`,e2=y))
structure(list(one = structure(13, .Dim = c(1L, 1L))), .Names = "one")
>dput(lapply(x,`+`,e2=1))
structure(list(one = 13), .Names = "one")


Prof. Ripley has pointed out this some time ago:
https://stat.ethz.ch/pipermail/r-devel/2011-October/062297.html

Note that ?`+` tells:

 The rules for determining the attributes of the result are rather
 complicated.  Most attributes are taken from the longer argument.
 Names will be copied from the first if it is the same length as
 the answer, otherwise from the second if that is.  If the
 arguments are the same length, attributes will be copied from
 both, with those of the first argument taking precedence when the
 same attribute is present in both arguments. For time series,
 these operations are allowed only if the series are compatible,
 when the class and ‘tsp’ attribute of whichever is a time series
 (the same, if both are) are used.  For arrays (and an array
 result) the dimensions and dimnames are taken from first argument
 if it is an array, otherwise the second.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

[Rd] matrices with names

2017-07-20 Thread William Dunlap via R-devel
How should R deal with matrices that have a 'names' attribute?  S (and S+)
did not allow an object to have both dims and names but R does.  However,
some R functions copy the dims but not the names to the returned value and
some copy both.  I don't see a pattern to it.  Is there a general rule for
when the names on a matrix should be copied to the return value of a
function?

> x <- matrix(11,1,1)
> names(x)<-"One"
> dput(x)
structure(11, .Dim = c(1L, 1L), .Names = "One")
> dput(log2(x))
structure(3.4594316186373, .Dim = c(1L, 1L), .Names = "One")
> dput(pchisq(x,8))
structure(0.798300801297471, .Dim = c(1L, 1L), .Names = "One")
> dput(x+1)
structure(12, .Dim = c(1L, 1L))
> dput(x > 3)
structure(TRUE, .Dim = c(1L, 1L))
> dput(!x)
structure(FALSE, .Names = "One", .Dim = c(1L, 1L))
> dput(-x)
structure(-11, .Dim = c(1L, 1L), .Names = "One")
> dput(0-x)
structure(-11, .Dim = c(1L, 1L))

The binary operators don't copy, unary operators do copy, and many other
low-level functions do copy.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel