我是R的新手,但经过一段时间努力解决这个问题后,我正在寻求帮助:
我有一个矩阵:
> 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"
我想将这个矩阵子集化为其他3个矩阵:
> 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)]
它适用于Ls1&amp; LS2。但是Ls3会改变格式。
> 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"
我不明白。
答案 0 :(得分:0)
默认[
选择尝试通过删除长度为1的维度来简化结果。如果您不想要,请添加drop = FALSE
。
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