-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
What if [[ relaxed the 1 element restriction #77
Comments
It's still type stable. But most things don't let [[ return more than 1 element so that would be weird. But you'd also be able to do x[[1:2]] to reproduce the base R behavior of x[1:2] |
The more I think about this the more I like it. [ Never reduces dimension. [[ Always does. But otherwise both can subset multiple elements |
Would obviously also allow x[[1:2]] <- 0 |
I like the idea and hope you don`t have to discard it due to interactions with other functions, packages, etc. Considering rray in isolation, this design seems clear and sound. This form of indexing would be almost innecesary if we had what I think is always usefull in an array package: the pair of functions to convert between indexes and positions in a given array (or dim). Havent looked for them in rray still, but supose they are somewhere |
I had some internal conversation with some rstudio employees about this yesterday. The consensus was that it would probably be better to have a separate function that might help convert a matrix of this form to positions in the array, rather than support an obscure form of indexing. xtensor has some helpers for this https://xtensor.readthedocs.io/en/latest/indices.html |
This seems to be as simple as flipping |
As in number of dimensions all arrays are really small, you can also afford the luxury of doing it in a "recursive R" oneliner if you will. rray_i2p <- function(mat,dim){
stopifnot(dim >= apply(mat,2,max))
if(ncol(mat) == 1) as.vector(mat) else
mat[,1]+dim[[1]]*(rray_i2p(mat[,2:ncol(mat),drop = FALSE], dim[-1])-1)
}
# Example
myIndexes <- matrix(c(3,1,3,2,1,2),ncol=3)
cbind(myIndexes,">", rray_i2p(myIndexes,c(3,5,2)))
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] "3" "3" "1" ">" "9"
#> [2,] "1" "2" "2" ">" "19" Created on 2019-04-11 by the reprex package (v0.2.1) |
If it used the full version of rray_yank() and rray_extract() then you could do
x[[is.na(x)]] <- 0
Which would be nice bc we don't allow that in [
The text was updated successfully, but these errors were encountered: