你好,我有一个关于R的快速问题。我有三个陈述:
n=187 a <- MatrixA[ (1):(4*n), c('x1','y') ] b <- MatrixA[ (1):(4*n), c('y') ]其中矩阵A是1250 x 2矩阵,第一列标记为“x1”,第二列标记为“y”。
我运行命令,a变成748 x 2 矩阵,但b是一个值 (它表示int [1:748] blablabla)。 为什么是这样? 我很困惑:这些陈述几乎完全相同。
我的最终目标是将b创建为具有列'y'值的748 x 1矩阵。 任何帮助表示感谢,并感谢您的阅读!
关于Rstudio:版本0.99.893 - ©2009-2016 RStudio,Inc。Mozilla / 5.0(Macintosh; Intel Mac OS X 10_11_3)AppleWebKit / 601.4.4(KHTML,和Gecko一样)
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)
最满意答案
在子集化时,如果可能,R将简化输出。 例如,如果您只选择矩阵的单个列,则默认情况下它将被强制转换为矢量。 详情请见此处 。
一个快速的解决方案是:
b <- MatrixA[ (1):(4*n), c('y'), drop=FALSE]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]更多推荐
发布评论