From dwinsemius at comcast.net  Sun Nov 12 18:02:23 2017
From: dwinsemius at comcast.net (David Winsemius)
Date: Sun, 12 Nov 2017 09:02:23 -0800
Subject: [Rd] Array changing address unexpectedly
In-Reply-To: <trinity-72a45fb5-062c-4f24-9082-4069285db083-1510505257888@3c-app-mailcom-bs15>
References: <trinity-72a45fb5-062c-4f24-9082-4069285db083-1510505257888@3c-app-mailcom-bs15>
Message-ID: <A6CB74E6-F1EF-4DA1-BACC-4C74ACCA9A7D@comcast.net>


> On Nov 12, 2017, at 8:47 AM, lille stor <lille.stor at gmx.com> wrote:
> 
> Hi,
> 
> Given the following R code:
> 
>     library(pryr)
> 
>     data <- array(dim = c(5))
> 
>     for(x in 1:5)
>     {
>          data[x] <- as.integer(x * 2)
>     }
> 
>     add = address(data) # save address of "data"
> 
>     for(x in 1:5)
>     {
>          data[x] <- as.integer(0)
>     }
> 
>     if (add == address(data))
>     {
>        print("Address did not change")
>     }
>     else
>     {
>        print("Address changed")
>     }
> 
> If one runs this code, message "Address changed" is printed. However, if one comments line "data[x] <- as.integer(0)" the address of "data" does not change and message "Address did not change" is printed instead. Why? The datatype of the array should not change with this line and hence no need for R to convert the array to a different type (and have the array's address changing in the process).

I'm guessing you didn't take note of the error message:

>     else
Error: unexpected 'else' in "    else"

It's always good practice to investigate errors. The else function needs to come immediately after the "{".

Here's a more complete test of what I take to be your question:

library(pryr)

data <- array(dim = c(5))
add = address(data)
    for(x in 1:5)
    {
         data[x] <- as.integer(x * 2)
    }
 if (add == address(data))
    {
       print("Address did not change")
    } else {
       print("Address changed")
    }


data <- array(dim = c(5))   # reset
add = address(data)
    for(x in 1:5)
    {
         data[x] <- as.integer(0)
    }

    if (add == address(data))
    {
       print("Address did not change")
    } else {
       print("Address changed")
    }

# changes in both situations.


> 
> Thank you!
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law



[Rd] Array changing address unexpectedly
David Winsemius