Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support .rx[row, col] indexing for matrices and data frames #1086

Open
Wainberg opened this issue Jan 28, 2024 · 1 comment
Open

Support .rx[row, col] indexing for matrices and data frames #1086

Wainberg opened this issue Jan 28, 2024 · 1 comment

Comments

@Wainberg
Copy link

Wainberg commented Jan 28, 2024

It would be nice to support matrix_or_df.rx[1, 2] as an alternative to matrix_or_df.rx(1, 2). This would also let you assign to a matrix or data frame using matrix_or_df.rx[1, 2] = ....

As an extension, it would also be nice to support matrix_or_df.rx[:, 2] as an alternative to matrix_or_df.rx(True, 2), and matrix_or_df.rx[2, :] as an alternative to matrix_or_df.rx(2, True). Note that this would mean matrix_or_df.rx[2, :] would behave differently from matrix_or_df.rx[2], which is unexpected for Python but makes sense when you consider that matrix_or_df[2,] and matrix_or_df[2] are different in R.

@Wainberg Wainberg changed the title Support matrix.rx[] indexing Support .rx[row, col] indexing for matrices and data frames Jan 28, 2024
@lgautier
Copy link
Member

lgautier commented Feb 11, 2024

Yes. I once considered it but the subseting operator [ in R can accept named arguments. The R documentation for [ has"

Usage:

     x[i]
     x[i, j, ... , drop = TRUE]
     x[[i, exact = TRUE]]
     x[[i, j, ..., exact = TRUE]]
     x$name
     getElement(object, name)
     
     x[i] <- value
     x[i, j, ...] <- value
     x[[i]] <- value
     x$name <- value

Python does not allow it. Adding a named argument to a [ call is a syntax error.

I checked this again and PEP-0472 could have resolved this but was ultimately rejected (https://peps.python.org/pep-0472/). Alternative methods propose a dictionary as an additional index to have named parameters. This could be a good solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants