rmatrixreplicate

Double values of a specific row in a matrix into the next row


I have this matrix a = [nrow = 365, ncol = 288] and I want to double the values of the 59th row. Then it means, the new matrix will be b = [nrow = 366, ncol = 288] and the values of the 59th & 60th row are the same. Is there any quick way to do that without doing the function?


Solution

  • Simply set the row indices, e.g.,

    a[c(1:59, 59:365),]
    

    or

    a[(k <- seq_len(nrow(a) + 1)) - (k > 59), ]
    

    or

    rbind(head(a, 59), tail(a, -58))