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!

zx8754
  • 52,746
  • 12
  • 114
  • 209
Sander
  • 51
  • 6

2 Answers2

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"
zx8754
  • 52,746
  • 12
  • 114
  • 209
0

Hard to say without example but try this:

B = as.matrix (c(A[2:6,1],A[11:16,1]))
SirSaleh
  • 1,452
  • 3
  • 23
  • 39