[R] For logical i and length(i) > length(x), x[i] <- value makes length(x) == length(i)
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@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.
Re: [R] For logical i and length(i) > length(x), x[i] <- value makes length(x) == length(i)
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 NewmillerThe . . Go Live... DCN:Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.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 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@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. __ R-help@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.
Re: [R] For logical i and length(i) > length(x), x[i] <- value makes length(x) == length(i)
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 NewmillerThe . . Go Live... DCN:<[hidden email]>Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.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.o
Re: [R] For logical i and length(i) > length(x), x[i] <- value makes length(x) == length(i)
Splus 8.2.0 (c. 2010) and Splus6.0 (c. 2001) act like R in this respect. x <- c(10.0, 11.0, 12.0) i <- c(FALSE,TRUE,FALSE,FALSE,TRUE) x[i] #[1] 11 NA x[i] <- 1:2 x #[1] 10 1 12 NA 2 I no longer have access to a running version of Splus 3.4, but it does not look like a feature we would have changed. Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Sep 12, 2015 at 9:17 AM, Suharto Anggono Suharto Anggono via R-help wrote: > 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 NewmillerThe . . Go Live... > DCN:<[hidden email]>Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/BatteriesO.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
Re: [R] For logical i and length(i) > length(x), x[i] <- value makes length(x) == length(i)
Thank you for looking. If the last element of i is TRUE, the largest value of which(i) is length(i). So, even if x[i] <- value were translated to x[which(i)] <- value, the result would have length(x) == length(i). Difference between x[i] <- value and x[which(i)] <- value in R would occur when the last element of i is FALSE, as in my original example: x <- NA x[c(FALSE,TRUE,FALSE)] <- 1 On Sun, 13/9/15, William Dunlap wrote: Subject: Re: [R] For logical i and length(i) > length(x), x[i] <- value makes length(x) == length(i) Cc: R-help@r-project.org Date: Sunday, 13 September, 2015, 9:22 AM Splus 8.2.0 (c. 2010) and Splus6.0 (c. 2001) act like R in this respect. x <- c(10.0, 11.0, 12.0) i <- c(FALSE,TRUE,FALSE,FALSE,TRUE) x[i] #[1] 11 NA x[i] <- 1:2 x #[1] 10 1 12 NA 2 I no longer have access to a running version of Splus 3.4, but it does not look like a feature we would have changed. Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Sep 12, 2015 at 9:17 AM, Suharto Anggono Suharto Anggono via R-help wrote: > 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 v
Re: [R] For logical i and length(i) > length(x), x[i] <- value makes length(x) == length(i)
>>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. The return value of which(i) gives only the indices of the TRUE values in i. It does not return the length of i. Hence x[which(i)] which is equivalent to w <- which(i) x[w] cannot return something that depends on length(i). Your inference is right for length(i)==length(x), which is the usual case. Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Sep 12, 2015 at 9:17 AM, Suharto Anggono Suharto Anggono via R-help wrote: > 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 NewmillerThe . . Go Live... > DCN:<[hidden email]>Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/BatteriesO.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.