[R] Error during wrapup: incorrect number of dimensions

Berend Hasselman bhh at xs4all.nl
Mon Mar 26 22:14:12 CEST 2012


On 26-03-2012, at 21:57, Sam Steingold wrote:

> when subsetting a matrix results in a single row, it is converted to a
> vector, not a matrix.
> how do I avoid this?
> 
> 1. __GOOD__
> 
>> edges <- get.edges(g,E(g))
>> edges
>      [,1] [,2]
> [1,]    0    2
> [2,]    0    3
> [3,]    0    4
> [4,]    0    5
> [5,]    1    1
> [6,]    0    4
> [7,]    0    6
> [8,]    0    7
> [9,]    0    8
> [10,]    0    9
> [11,]    0    5
> [12,]    0   10
> [13,]    0   11
> [14,]    0   12
> [15,]    0   11
> [16,]    0   13
> [17,]    0   14
> [18,]    0   15
> [19,]    0   16
> [20,]    1   17
> [21,]    1   11
> [22,]    0   11
>> loops <- edges[,1] == edges[,2]
>> loopless <- edges[!loops,]
>> loopless
> loopless
>      [,1] [,2]
> [1,]    0    2
> [2,]    0    3
> [3,]    0    4
> [4,]    0    5
> [5,]    0    4
> [6,]    0    6
> [7,]    0    7
> [8,]    0    8
> [9,]    0    9
> [10,]    0    5
> [11,]    0   10
> [12,]    0   11
> [13,]    0   12
> [14,]    0   11
> [15,]    0   13
> [16,]    0   14
> [17,]    0   15
> [18,]    0   16
> [19,]    1   17
> [20,]    1   11
> [21,]    0   11
>> unique(loopless[,1])
> [1] 0 1
> 
> 2. __BAD__
>> edges
>     [,1] [,2]     
> [1,]    0    1
> [2,]    0    0
> Browse[1]> edges[!loops,]
> [1] 0 1


?`[`

Use drop=FALSE

Berend



More information about the R-help mailing list