-1

I'm a newbie regarding R, but after struggling with this problem for quite some time I'm searching for help:

I have a matrix:

> is.matrix(Ls)
[1] TRUE

> Ls
       Start        Duration
       newrow_fin "1014507296" "133.313"
       newrow_fin "1014657293" "449.975"
       newrow_fin "1015523823" "566.614"
       newrow_fin "1017473690" "233.236"

And I would like to subset this matrix into 3 other matrices:

> int2_start
 [1] 1014174109
> int3_start
 [1] 1016639945
Ls1<-subset.matrix(Ls, (Ls[,1])<=int2_start) [,c(1,2)]
Ls2<-subset.matrix(Ls, (Ls[,1])>int2_start) [,c(1,2)]
Ls2<-subset.matrix(Ls2, (Ls2[,1])<=int3_start)  [,c(1,2)]
Ls3<-subset.matrix(Ls, (Ls[,1])>int3_start) [,c(1,2)]

It works fine for Ls1 & Ls2. But Ls3 changes format.

> Ls1
 Start Duration
> Ls2
       Start        Duration
       newrow_fin "1014507296" "133.313"
       newrow_fin "1014657293" "449.975"
       newrow_fin "1015523823" "566.614"
> Ls3
   Start     Duration
       "1017473690"    "233.236"
> is(Ls1)
[1] "matrix"    "array"     "structure" "vector"
> is(Ls2)
[1] "matrix"    "array"     "structure" "vector"
> is(Ls3)
[1] "character"           "vector"              "data.frameRowLabels" "SuperClassMethod" 

I don't get it.

1 Answers1

0

By default [ selections try to simplify the result by dropping dimension with length 1. Add drop = FALSE if you don't want that.

test <- array(1, dim = rep(2, 3))
dim(test) # 3 dimensions
dim(test[1, ,]) # 2 dimensions
dim(test[1, 1,]) # 1 dimension
dim(test[1, , , drop = FALSE]) # 3 dimensions
dim(test[1, 1, , drop = FALSE]) # 3 dimensions
Thierry
  • 18,049
  • 5
  • 48
  • 66