1
votes

Hi there I have a quick question about R. I have three statements:

    n=187
    a <- MatrixA[ (1):(4*n), c('x1','y') ]
    b <- MatrixA[ (1):(4*n), c('y') ]

where Matrix A is a 1250 x 2 matrix with the first column labeled 'x1' and the second column labeled 'y'.

I run the command and a becomes a 748 x 2 matrix however b is a value (it says int[1:748] blablabla).
Why is this? I am confused: these statements are almost identical.

My end goal is to create b as a 748 x 1 matrix with the values of column 'y'.
Any help appreciated and thank you for reading!!

my about Rstudio: Version 0.99.893 – © 2009-2016 RStudio, Inc. Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko)

1

1 Answers

3
votes

When subsetting, R will simplify the output if possible. For example, if you only pick a single column of a matrix, by default it will be coerced to a vector. See here for details.

A quick solution is to do:

b <- MatrixA[ (1):(4*n), c('y'), drop=FALSE]