了解内置功能非列表
我很感谢您在r linist()
在R中工作的帮助,因为当我尝试将列表转换为使用此列表时,我会遇到一些问题。我的问题:
当我在列表上使用它lst
与原始输出## [1,] -0.4111434 -0.1284589 -0.1824016 0.4634135列向量而不是行向量。当我试图计算方程式
平均%*%t(linist(lst))
时,我发现了这一点(平均值是带有预期返回的行矩阵0.001083249 0.001846313 0.001846313 0.002450989 0.00494948962 0.005279962 0.0052799165
),它给了我一个5x5矩阵而不是数字。
我已经使用了相同的方程平均%*%t(vec)
用于其他向量,包括vector ## [1,] 0.06332585 0.07328322 0.08528878 0.2380229 0.2380229 0.5400793 0.5400793
和IT我是数值的价值,而不是矩阵。
因此,我想知道lins()
在R中的工作方式,以及为什么我获得矩阵而不是数值值。是因为它创建了列向量吗?如果是,我可以将其变成行矢量吗?
I would appreciate help with how the function unlist()
works in R, since I encounter some problems when I try to convert a list to a vector with this. My problem:
When I used this on my list lst
with the original output ## [1,] -0.4111434 -0.1284589 -0.1824016 0.4634135 1.25859
it seems to be created into a column vector rather than a row vector. I discovered this when I was trying to calculate the equation mean%*%t(unlist(lst))
(mean is a row-matrix with expected returns 0.001083249 0.001846313 0.002450989 0.004948962 0.005279165
) and it gave me a 5x5 matrix instead of a number.
I have used the same equation mean%*%t(vec)
for other vectors, including for the vector ## [1,] 0.06332585 0.07328322 0.08528878 0.2380229 0.5400793
and it gave me a numerical value and not a matrix.
Hence I wonder how unlist()
works in R, and why I get a matrix instead of a numerical value. Is it because it creates a column vector? If yes, can I turn it into a row vector instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
评论太久了。
在线性代数当然是“行矢量”和“列向量”的概念。当您将行矢量乘以列向量时,您将获得一个数字(点产品)。当您将列向量乘以行向量时,您将获得N X N矩阵。
但是
vector
r中的数据类型并非如此。在r中,向量是向量,没有“行”或“列”(认为它是有序集合)。但是,R中有一个矩阵
数据类型,当然,您可以具有带有一行的矩阵,也可以使用一个列的矩阵。这些行为就像您称为行/列向量的内容。UNLIST(...)
函数在RList
上运行。如果您传递了不是列表的东西,它只会返回原始对象。因此,例如,如果提供N X K矩阵,则可以返回N X K矩阵:转置函数,
t(...)
返回矩阵,但可以做出某些假设。如果您通过R向量,它将返回带有一行的矩阵,从本质上讲,将向量转换为带有一列的矩阵,然后对其进行转换。最后,矩阵乘法运算符,
%*%
也是假设。故事的寓意是避免使用(r)向量和矩阵,结果是完全确定性的,但可能不是您所期望的。例如,如果您有两个向量,
a
和b
并想要点产品,我将使用sum(a*b)
。Too long for a comment.
In linear algebra there is of course the concept of "row vector" and "column vector". When you multiply a row vector by a column vector you get a single number (the dot product). When you multiply a column vector by a row vector you get an n X n matrix.
But the
vector
data type in R does not behave that way. In R a vector is a vector, there is no "row" or "column" to it (think of it like an ordered collection). There is, however, amatrix
data type in R, and of course you can have a matrix with one row, or a matrix with one column. These behave like what you are calling row/column vectors.The
unlist(...)
function operates on an Rlist
. If you pass something that isn't a list, it just returns the original object. So for instance if you provide an n X k matrix you get back an n X k matrix:The transpose function,
t(...)
returns a matrix, but makes certain assumptions. If you pass an R vector it returns a matrix with one row, in essence converting the vector to a matrix with one column, and transposing that.Finally, the matrix multiplication operator,
%*%
also makes assumptions.The moral of the story is to avoid using (R) vectors and matrices together, the results are completely deterministic, but may not be what you expect. For instance if you have two vectors,
a
andb
and want the dot product, I'd usesum(a*b)
.