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

Generic support for dimension-aware attributes, e.g. colattr(x, 'gender') #2

Open
HenrikBengtsson opened this issue Apr 14, 2015 · 2 comments

Comments

@HenrikBengtsson
Copy link
Owner

Description

Generic support for dimension-aware attributes that are acknowledged whenever the object is subsetted. For vectors we have names(), for matrices and data frames we have rownames() and colnames(), and for arrays and other objects we have dimnames().

Example

> x <- matrix(1:12, ncol=4)
> colnames(x) <- c("A", "B", "C", "D")
> colattr(x, 'gender') <- c("male", "male", "female", "male")
> colattr(x, 'age') <- c(26, 43, 28, 33)
> x

     male male female male
       26   43     28   33
        A    B      C    D
[1,]    1    4      7   10
[2,]    2    5      8   11
[3,]    3    6      9   12

> y <- x[,2:3]
> y
     male female
       43     28
        B      C
[1,]    4      7
[2,]    5      8
[3,]    6      9

> colnames(y)
[1] B C
> colattr(y, 'name')
[1] B C
> colattr(y, 'gender')
[1] "male" "female"
> colattr(y, 'age')
[1] 43 28
@HenrikBengtsson
Copy link
Owner Author

ML commented (2015-11-14):

This is essentially Biobase::AnnotatedDataFrame and S4Vectors::DataFrame. One interesting direction would be to consider the meta columns as grouping factors and use them to implement pivot-table functionality.

@karoliskoncevicius
Copy link

karoliskoncevicius commented Aug 15, 2021

I have an alternative package that tries to address this issue for matrices: annmatrix. AnnotatedDataFrame and BioConductor objects in general were a bit too heavy for my taste, so I tried to construct a matrix-based custom S3 object. It seems to work quite well, better than I initially imagined. One major upside is that the object is still a proper matrix and can be passed to functions expecting a matrix.

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

No branches or pull requests

2 participants