You can use order
:
diag(input_matrix) <- NA
k <- 2
t(apply(input_matrix, 1, function(x)
replace(x, -order(x, decreasing = TRUE)[1:k], 0)))
# [,1] [,2] [,3] [,4]
#row1 0.00 0.2 0.28 0.0
#row2 0.20 0.0 0.00 -0.5
#row3 0.28 0.0 0.00 0.3
#row4 -0.11 0.0 0.30 0.0
CLICK HERE to find out more related problems solutions.