简体   繁体   中英

Scaling a Single Column Vector of a Matrix in R

I'm interested in scaling one column of a matrix and then returning the whole matrix.

Suppose I have the matrix:

矩阵

If I do something like:

> A[,1] * 10

I'll end up with:

在此处输入图片说明

However, I'd like to end up with something like:

在此处输入图片说明

Here's the solution I've come up with so far:

> A <- cbind(A[,1]*10, A[,2])
> A

Is this the best way to accomplish what I'd like to do?

You can assign it to the columns that is being changed

A[,1] <- A[,1]*10
A
#     [,1] [,2]
#[1,]   10    2
#[2,]   30    4

data

A <- matrix(c(1,3,2,4), ncol=2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM