了解内置功能非列表

发布于 2025-02-07 04:48:28 字数 638 浏览 0 评论 0原文

我很感谢您在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2025-02-14 04:48:28

评论太久了。

线性代数当然是“行矢量”和“列向量”的概念。当您将行矢量乘以列向量时,您将获得一个数字(点产品)。当您将列向量乘以行向量时,您将获得N X N矩阵。

但是vector r中的数据类型并非如此。在r中,向量是向量,没有“行”或“列”(认为它是有序集合)。但是,R中有一个矩阵数据类型,当然,您可以具有带有一行的矩阵,也可以使用一个列的矩阵。这些行为就像您称为行/列向量的内容。

UNLIST(...)函数在R List上运行。如果您传递了不是列表的东西,它只会返回原始对象。因此,例如,如果提供N X K矩阵,则可以返回N X K矩阵:

m <- matrix(1:9, nc=3)
unlist(m)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

转置函数,t(...)返回矩阵,但可以做出某些假设。如果您通过R向量,它将返回带有一行的矩阵,从本质上讲,将向量转换为带有一列的矩阵,然后对其进行转换。

v <- 1:5    # R vector
t(v)        # returns R matrix with one row
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5

最后,矩阵乘法运算符,%*%也是假设。

x <- 1:5   # R vector
y <- 6:10  # R vector
x %*% y    # converts x to 1-row matrix, y to 1-column matrix
##      [,1]
## [1,]  130

z <- matrix(1:5, nr=1)  # R matrix with one row
z %*% y                 # converts y to 1-column matrix
##      [,1]
## [1,]  130

y %*% z                 # converts y to 1-column martix
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    6   12   18   24   30
## [2,]    7   14   21   28   35
## [3,]    8   16   24   32   40
## [4,]    9   18   27   36   45
## [5,]   10   20   30   40   50

故事的寓意是避免使用(r)向量和矩阵,结果是完全确定性的,但可能不是您所期望的。例如,如果您有两个向量,ab并想要点产品,我将使用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, a matrix 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 R list. 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:

m <- matrix(1:9, nc=3)
unlist(m)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

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.

v <- 1:5    # R vector
t(v)        # returns R matrix with one row
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5

Finally, the matrix multiplication operator, %*% also makes assumptions.

x <- 1:5   # R vector
y <- 6:10  # R vector
x %*% y    # converts x to 1-row matrix, y to 1-column matrix
##      [,1]
## [1,]  130

z <- matrix(1:5, nr=1)  # R matrix with one row
z %*% y                 # converts y to 1-column matrix
##      [,1]
## [1,]  130

y %*% z                 # converts y to 1-column martix
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    6   12   18   24   30
## [2,]    7   14   21   28   35
## [3,]    8   16   24   32   40
## [4,]    9   18   27   36   45
## [5,]   10   20   30   40   50

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 and b and want the dot product, I'd use sum(a*b).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文