[R] For logical i and length(i) > length(x), x[i] <- value makes length(x) == length(i)

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sun Sep 6 00:49:52 CEST 2015


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:<jdnewmil at dcn.davis.ca.us>        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 <r-help at r-project.org> 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
>
>______________________________________________
>R-help at r-project.org 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