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

43 Views Asked by At

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?

2

There are 2 best solutions below

0
ThomasIsCoding On BEST ANSWER

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))
0
Caro On

Assuming a dataframe called df

# Duplicate the values of the 59th row
duplicate_row <- df[59, ]

# Then insert the duplicated row after the 59th row
b <- rbind(df[1:59, ], duplicate_row, df[60:nrow(df), ])