How to swap a number of the values between 2 rows in R

筅森魡賤 提交于 2019-12-23 04:51:43

问题


I have a matrix with the size of 10x100. How can I swap the values between row 1 and row 2 in the first 30% of the columns?


回答1:


We can just reverse the row index for the 1st two rows along along with column index created by taking the sequence of rounded 30% total number of columns for swapping the values in the rows.

colS <- seq(round(ncol(m1)*0.3))
m1[2:1, colS] <- m1[1:2, colS]

data

m1 <- matrix(1:1000, 10, 100)


来源:https://stackoverflow.com/questions/38805833/how-to-swap-a-number-of-the-values-between-2-rows-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!