1

I have matrix A with 1 column, and I want to create a new matrix B with some numbers from A. More precisely

B[1-10] <- A[2-6, and 11-16]

Do anyone know how to do this?

Thanks in advance!

3

2 Answers 2

4

Say we have this example matrix:

# example 1 column matrix
A <- matrix(1:20, ncol = 1)

We can subset the 1st column and selected rows:

B <- A[ c(2:6, 11:16), 1 ]
dim(B)
# NULL
class(B)
# [1] "integer"

Notice above will give us an integer vector. To keep it as matrix after subsetting use drop = FALSE:

B <- A[ c(2:6, 11:16), 1, drop = FALSE ]
dim(B)
# [1] 11  1
class(B)
# [1] "matrix"
0

Hard to say without example but try this:

B = as.matrix (c(A[2:6,1],A[11:16,1]))
3
  • 1
    A[11-16,1] is same as A[-5,1] meaning exclude 5th row.
    – zx8754
    Commented May 16, 2018 at 13:25
  • 3
    This does not give desired output
    – Cath
    Commented May 16, 2018 at 13:46
  • Opps. Just a typoe. I mean 11:16. @zx8754
    – SirSaleh
    Commented May 16, 2018 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.