Hi David,
?
Thanks for the correction concerning the "else" issue.
?
Taking your code and removing some lines (to increase readability):
?
library(pryr)
?
data <- array(dim = c(5))
for(x in 1:5)
{
?? data[x] <- as.integer(x * 2)
}
?
#print(data)
?
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")
}
?
?
If one runs this code everything works as expected, i.e. message "Address
did not change" is printed. However, if one uncomments line
"#print(data)", message "Address is changed" is printed
instead. Any idea why this happens as it is a bit counter-intuitive? Is it
something to do with some kind of lazy-evaluation mechanism R has that makes the
array to be filled-up only when needed (in this case, when printing it) thus
changing the array's address?
?
Thank you once more!
?
?
Sent:?Sunday, November 12, 2017 at 6:02 PM
From:?"David Winsemius" <dwinsemius at comcast.net>
To:?"lille stor" <lille.stor at gmx.com>
Cc:?r-devel at r-project.org
Subject:?Re: [Rd] Array changing address unexpectedly> 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
?