[R] For logical i and length(i) > length(x), x[i] <- value makes length(x) == length(i)
Suharto Anggono Suharto Anggono
suharto_anggono at yahoo.com
Sat Sep 12 18:17:01 CEST 2015
I'll make my points clearer.
- The case of logical index vector longer than the original vector is not usual case for me. As I said, I encountered it in the code of function 'rank' in R.
- Before, I have read "S-PLUS help" (version 3.4, it seems), http://www.uni-muenster.de/ZIV.BennoSueselbeck/s-html/helpfiles/Subscript.html. The following is the relevant part. There are missing pieces there that I fill by guessing.
Vector subscripts are generated with when i and x are both vectors. (....) The result of the expression is to extract or replace elements of x corresponding to a vector of positive indices computed according to the value of i.
.... If i is logical the indices are produced by starting at 1 and selecting the numbers for which the corresponding element is T. If is shorter than it is extended by cyclic repetition. It can be longer than as well, with no change in the computation of indices. ....
....
For replacements, x[i] <- value the rule is that the length of will be set to the largest value in the indices, if that is bigger than the current length of x. ....
>From it, I infer that, if i is logical and length(i) >= length(x), x[i] <- value has the same effect to x[which(i)] <- value, where which(i) takes indices where i is T.
- In R, if i is logical and length(i) > length(x), length(x) after x[i] <- value may be different from after x[which(i)] <- value.
- So, I wonder if R inherits the behavior from S or not.
- The behavior is not clearly documented in R. I just find "R Language Definition", "3.4.1 Indexing by vectors", that can be interpreted to imply the behavior.
- However, for a particular case, function 'rank' in R relies on the behavior.
However, it seems that relying on the behavior is not on purpose. Previously, at least until R 3.1.3, the code of function 'rank' has the following before yy <- NA .
yy <- integer(length(x))
storage.mode(yy) <- storage.mode(y)
It seems that yy[] <- NA is what is intended.
- However, for me, the behavior is plausible. The assumption is that indices from 1 to length(i) exist.
--------------------------------------
I think this behavior is consistent with typical indexing behaviour in R... I would ask you what result you thought you should get? I, for one, can think of all sorts of uses for numeric indexes that have different lengths than the vector, but am stumped to think of any use for what you are proposing.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<[hidden email]> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On September 5, 2015 10:02:05 AM PDT, Suharto Anggono Suharto Anggono via R-help <[hidden email]> wrote:
>I came across this behavior when I followed the code of function 'rank'
>in R.
>
>It seems that subassignment of a vector by a logical index vector that
>is longer than the original vector always results in expanding the
>original vector to the length of the index vector.
>
>The resulting length may be different from the result of subassignment
>by the equivalent numeric vector. For subassignment of a vector by a
>numeric index vector, the original vector is expanded to the maximum
>index, if it is larger than the length of the original vector.
>
>This is an example.
>
>> x <- NA
>> x[c(FALSE,TRUE,FALSE)] <- 1
>> x
>[1] NA 1 NA
>
>Compare to this.
>
>> x <- NA
>> x[which(c(FALSE,TRUE,FALSE))] <- 1
>> x
>[1] NA 1
>
>Does S exhibit the same behavior?
>
>Currently, if there is NA and na.last = "keep", function 'rank' in R
>relies on this behavior to give correct result length.
>
>In "R Language Definition", "3.4.1 Indexing by vectors" says: "Logical.
>The indexing i should generally have the same length as x. .... If it
>is longer, then x is conceptually extended with NAs. ...." The
>statement can be taught to support the observed behavior.
>
>> sessionInfo()
>R version 3.2.2 (2015-08-14)
>Platform: i386-w64-mingw32/i386 (32-bit)
>Running under: Windows XP (build 2600) Service Pack 2
>
>locale:
>[1] LC_COLLATE=English_United States.1252
>[2] LC_CTYPE=English_United States.1252
>[3] LC_MONETARY=English_United States.1252
>[4] LC_NUMERIC=C
>[5] LC_TIME=English_United States.1252
>
>attached base packages:
>[1] stats graphics grDevices utils datasets methods base
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list