如何在 R 中的 2 行之间交换多个值 [英] How to swap a number of the values between 2 rows in R

查看:72
本文介绍了如何在 R 中的 2 行之间交换多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大小为 10x100 的矩阵.如何在前 30% 的列中交换第 1 行和第 2 行之间的值?

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 两行的行索引连同通过采用 rounded 30 的序列创建的列索引一起反转% 用于交换行中值的总列数.

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]

数据

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

这篇关于如何在 R 中的 2 行之间交换多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆