[R] .Alias

Jens Oehlschlägel jens.oehlschlaegel at bbdo-interone.de
Thu Oct 26 16:30:46 CEST 2000


> You can only .Alias entire R objects, and x[9] is not one, it is an
> expression which can be evaluated to an R object and you're
> effectively .Aliasing the unnamed result of that evaluation.

the help file says:

  `.Alias' creates an alias to another (part of) an R object
                                      ^^^^^^^
so may be the help file needs a fix (but see at the end of my mail)


However, even with entire objects I still don't understand in which cases
the alias does NOT break into a copy.

> x <- 9:1
> y <- .Alias(x)
> x[1] <- 10
> y
[1] 9 8 7 6 5 4 3 2 1
# broken

> x <- 9:1
> y <- .Alias(x)
> y[1] <- 10
> x
[1] 9 8 7 6 5 4 3 2 1
#broken

## ok, got it, this is again a change in structure as 9:1 is integer and 10
is double !!
> is.integer(x)
[1] TRUE
> is.integer(y)
[1] FALSE

# and finally those two work:
> x <- 9:1
> y <- .Alias(x)
> x[1]
[1] 9
> x[1] <- 10:10
> y
[1] 10  8  7  6  5  4  3  2  1
# as expected

> x <- 9:1
> y <- .Alias(x)
> y[1] <- 10:10
> x
[1] 10  8  7  6  5  4  3  2  1
# as expected


#### Well, the part thing is indeed more complicated, lets look at lists

## the [ operator

> x <- list(a=1, b=2)
> y <- .Alias(x[1])
> y
$a
[1] 1
> y$a <- 10
> x
$a
[1] 1

$b
[1] 2
# broken


> x <- list(a=1, b=2)
> y <- .Alias(x[1])
> x[1] <- list(a=10)
> y
$a
[1] 1
# broken


## the [[ operator
2
> x <- list(a=1, b=2)
> y <- .Alias(x[[1]])
> y
[1] 1
> y[1] <- 10
> x
$a
[1] 1

$b
[1] 2
# broken

> x <- list(a=1, b=2)
> y <- .Alias(x[[1]])
> x[[1]] <- 10
> y
[1] 1
# broken



## the $ operator

> x <- list(a=1, b=2)
> y <- .Alias(x$a)
> y
[1] 1
> x$a <- 10
> y
[1] 1
# broken

> x <- list(a=1, b=2)
> y <- .Alias(x$a)
> y[1] <- 10
> x
$a
[1] 10

$b
[1] 2
# NOT broken !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


quite unpredictable, isn't it


Jens

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list