简体   繁体   中英

Attaching a row of numbers/ a vector to an existing Column of a Matrix in R

I know that this is a rather simple question but I havent been able to find an answer and its driving me insane.

I have a Matrix with two columns:

   [,1][,2]
[1,]  0   1
      0   2
      0   3

I want to add a sequence of numbers to the second column( for example 4,5,6) so that it becomes:

    [,1][,2]
[1,]  0   1
      0   2
      0   3
      0   4
      0   5
      0   6

If I try:

 Matrix[,2]<-rbind(c(4,5,6))
 Matrix[,2]<-c(4,5,6)     

and simillar stuff I get an error or it overrides all previous numbers. Im asking because I want to create a Matrix with two columns in one of which the results of a contious logistic function with different r-values need to be saved. If you want to help me fix my travesty of code I would be very grateful. Here ist the code sample I need help with:

rdvec<-c(seq(from=1,to=3,by=0.02))
vec<-numeric()
for (i in 1:length(rdvec)){
    rd<-rdvec[i]
    vec<-logfun(N0, rd, K, schritte) # the logistic function
    vec<-vec[-c(1:100)] # i only need the second half of the results
    # and this is where i need help creating/updating a matrix
    Matrix[,2]<-rbind(Matrix,vec) # Ive tried this and variations of it but it obviously 
                                    doesnt work
}

Any help you can give me is appreciated.

If the intention is to expand the rows of the data, an option is to create a sequence of index of rows from the last row (+1) to the length of the vector ('n2') (-1), create the sequence ( : ), and assign those new rows in a data.frame with the two column created by cbind ing 0 and the vec tor

n1 <- (nrow(Matrix) + 1)
n2 <- n1 + length(vec)-1
d1 <- as.data.frame(Matrix)
d1[n1:n2,] <- cbind(0, vec)
d1

Or another way it to rbind and update the same object

Matrix <- rbind(Matrix, cbind(0, vec))

data

Matrix <- cbind(0, 1:3)
vec <- 4:6

You can also use the following solution if you still insist on using a for loop. However, the other solutions proposed by dear @akrun is much more efficient:

# Here is your original matrix
mx <- matrix(c(0, 0, 0, 1, 2, 3), ncol = 2)

# I defined a custom function that takes a matrix and the vector to be added
# to the second column of the matrix

fn <- function(mx, vec) {
  out <- matrix(rep(NA, 2 * length(vec)), ncol = 2)
  
  for(i in 1:nrow(out)) {
    out[i, ] <- mx[nrow(mx), ] + c(0, i)
  }
  rbind(mx, out)
}

fn(mx, c(4, 5, 6))

     [,1] [,2]
[1,]    0    1
[2,]    0    2
[3,]    0    3
[4,]    0    4
[5,]    0    5
[6,]    0    6

We could do:

x <- matrix(1:6, 6, 2)
y <- matrix(rep( 0, len=6), nrow = 6)
x[,1] <- y[,1]
x

Output:

     [,1] [,2]
[1,]    0    1
[2,]    0    2
[3,]    0    3
[4,]    0    4
[5,]    0    5
[6,]    0    6

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