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

Preserve element names in multi-dimensional subsetting #5

Open
HenrikBengtsson opened this issue Dec 29, 2015 · 0 comments
Open

Preserve element names in multi-dimensional subsetting #5

HenrikBengtsson opened this issue Dec 29, 2015 · 0 comments

Comments

@HenrikBengtsson
Copy link
Owner

Background

Vectors in can have dimensions and corresponding names, cf. dim() and dimnames(). They can concurrently have element names, cf. names(). For instance,

> x <- matrix(1:6, nrow=2)
> rownames(x) <- c("A", "B")
> colnames(x) <- c("X", "Y", "Z")
> names(x) <- letters[1:6]
> x
  X Y Z
A 1 3 5
B 2 4 6
attr(,"names")
[1] "a" "b" "c" "d" "e" "f"

This allows us to access elements both by dimensional names as by element names, e.g.

> x[3]
c
3
> x["c"]
c
3
> x[1,2]
[1] 3
> x["A","Y"]
[1] 3

Problem

When subsetting by dimensions, we loose the element names, e.g.

> y <- x[,2:3]
> y
  Y Z
A 3 5
B 4 6
> names(y)
NULL

Wish

Preserve element names also when subsetting by dimensions, e.g.

> y
  Y Z
A 3 5
B 4 6
attr(,"names")
[1] "c" "d" "e" "f"
> y[1]
c
3
> y["c"]
c
3
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

1 participant