There is a R dataframe, I want to filter out the top Nth value of each row with the rest values being set to 0.
For example, suppose the dataframe looks like the following table:
0.5 0.3 0.2 0.15 0.9
0.3 0.1 0.25 0.4 0.14
0.6 0.7 0.35 0.2 0.1
I want to keep the top 2 value of each row, and the rest should be set to 0. Then, the previous dataframe should be transformed into the following one:
0.5 0 0 0 0.9
0.3 0 0 0.4 0.14
0.6 0.7 0 0 0
Many thanks for your help 🙂