R矩阵创建(R matrix creating)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
R矩阵创建(R matrix creating)

你好,我有一个关于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]

更多推荐

本文发布于:2023-04-17 09:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/7e6f2732f2525b98cbe57a7851dd2f31.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   matrix   creating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!