Hi all, I tended to use rbind, or cbind to force a vector be be deemed as a column or row vector. This is very important if I want to do things like u' * A * u, where u' is a row vector and u is a column vector, regardless of what originall format the "u" is... I want to recast it to column vector or row vector... How can I do that? ---------------------------------------------------- b_hat=solve(t(X) %*% X) %*% t(X) %*% Y; attr(b_hat, "dim") [1] 4 1 rbind(b_hat) [,1] -4.814763 V -5.804245 -5.122668 -4.308326 [[alternative HTML version deleted]]
Gabor Grothendieck
2006-Feb-02 08:00 UTC
[R] How to force a vector to be column or row vector?
Try this: rbind(c(x)) cbind(c(x)) t(c(x)) On 2/2/06, Michael <comtech.usa at gmail.com> wrote:> Hi all, > > I tended to use rbind, or cbind to force a vector be be deemed as a column > or row vector. This is very important if I want to do things like u' * A * > u, where u' is a row vector and u is a column vector, regardless of what > originall format the "u" is... I want to recast it to column vector or row > vector... How can I do that? > > ---------------------------------------------------- > > > b_hat=solve(t(X) %*% X) %*% t(X) %*% Y; > > attr(b_hat, "dim") > [1] 4 1 > > rbind(b_hat) > [,1] > -4.814763 > V -5.804245 > -5.122668 > -4.308326 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Hi, Perhaps you should try the transpose function t(). It converts a row into a column and vice versa. HTH, Martin Lam --- Michael <comtech.usa at gmail.com> wrote:> Hi all, > > I tended to use rbind, or cbind to force a vector be > be deemed as a column > or row vector. This is very important if I want to > do things like u' * A * > u, where u' is a row vector and u is a column > vector, regardless of what > originall format the "u" is... I want to recast it > to column vector or row > vector... How can I do that? > > ---------------------------------------------------- > > > b_hat=solve(t(X) %*% X) %*% t(X) %*% Y; > > attr(b_hat, "dim") > [1] 4 1 > > rbind(b_hat) > [,1] > -4.814763 > V -5.804245 > -5.122668 > -4.308326 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
From: Gabor Grothendieck> > Try this: > > > rbind(c(x)) > cbind(c(x)) > t(c(x))The c() is not needed above. Also, this might be a better way of computing a quadratic form:> x <- 1:3 > m <- matrix(sample(1:9), 3, 3) > crossprod(crossprod(m, x), x)[,1] [1,] 202> t(x) %*% m %*% x # Just checking...[,1] [1,] 202 In R, it usually makes little difference whether a vector is row-vector or column-vector. Andy> On 2/2/06, Michael <comtech.usa at gmail.com> wrote: > > Hi all, > > > > I tended to use rbind, or cbind to force a vector be be > deemed as a column > > or row vector. This is very important if I want to do > things like u' * A * > > u, where u' is a row vector and u is a column vector, > regardless of what > > originall format the "u" is... I want to recast it to > column vector or row > > vector... How can I do that? > > > > ---------------------------------------------------- > > > > > > b_hat=solve(t(X) %*% X) %*% t(X) %*% Y; > > > > attr(b_hat, "dim") > > [1] 4 1 > > > > rbind(b_hat) > > [,1] > > -4.814763 > > V -5.804245 > > -5.122668 > > -4.308326 > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Gabor Grothendieck
2006-Feb-02 14:43 UTC
[R] How to force a vector to be column or row vector?
On 2/2/06, Liaw, Andy <andy_liaw at merck.com> wrote:> From: Gabor Grothendieck > > > > Try this: > > > > > > rbind(c(x)) > > cbind(c(x)) > > t(c(x)) > > The c() is not needed above.The poster asked for an expression that would work "regardless of the original form" of the vector -- row, column or neither. The c gets it into a known form first. For example:> cbind(matrix(1,1,3)) # not a column vector![,1] [,2] [,3] [1,] 1 1 1