Bug 15301 - Subsetting 1-D array x by empty index erases labels if length(x) > 1 or (length(x) == 1 and drop=FALSE)
Subsetting 1-D array x by empty index erases labels if length(x) > 1 or (leng...
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Language
R 3.0.0
ix86 (32-bit) Windows 32-bit
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-05-01 12:14 UTC by Suharto Anggono
Modified: 2013-07-15 20:11 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Suharto Anggono 2013-05-01 12:14:21 UTC
This is about subsetting one-dimensional array by empty index in R.

--------
Case 1: one-dimensional array of length greater than 1

> x <- array(0, dim=2, dimnames=list(d1=LETTERS[1:2])); x
d1
A B
0 0
> x[]
[1] 0 0
> attributes(x[])
$dim
[1] 2

$dimnames
$dimnames$d1
NULL


Above, in x[], labels (A, B) is lost.
x[] is still a one-dimensional array, but dimnames(x[])[[1]] is NULL.

Usually, in subsetting, labels are kept where possible.

x[] takes all, like x[1:2].

> x[1:2]
d1
A B
0 0

In x[1:2], labels (A, B) is present.

--------
Case 2: one-dimensional array of length 1

> x <- array(0, dimnames=list(d1="A")); x
d1
A
0
> x[]
d1
A
0
> x[, drop=FALSE]
[1] 0
> attributes(x[, drop=FALSE])
$dim
[1] 1

$dimnames
$dimnames$d1
NULL


Above, in x[, drop=FALSE], label (A) is lost.

For comparison:

> x[1]
A
0
> x[1, drop=FALSE]
d1
A
0

--------
> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: i386-w64-mingw32/i386 (32-bit)

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


The earliest version of R that I have is 2.5.1. I see the same there.