编写一个函数,它接受一个长度为2的参数x,并返回x的有序值。也就是说,如果x2<x1,则函数应返回c(x2,x1),否则应返回x。
使用您的函数处理一个包含2列的数据集,如下所示。在数据集的行上迭代,如果第i行第二列的元素小于第i行第一列的元素,则通过对刚刚编写的函数进行适当的调用来切换行中两个条目的顺序。(编写并使用自己的函数。不要使用内置函数order()或rev()。)使用以下数据进行测试。
Write a function which takes one argument x of length 2, and returns the ordered values of x. That is,
if x2 < x1, your function should return c(x2, x1), otherwise it should return x.
Use your function to process a dataset with 2 columns as follows. Iterate over the rows of the data set, and if
the element in the 2nd column of row i is less than the element in the first column of row i, switch the order
of the two entries in the row by making a suitable call to the function you just wrote. (Write and use your
own function. do NOT use the built in function order() or rev().)
Test using the following data.
set.seed(133)
data <- matrix(rnorm(20), byrow = T, ncol = 2)
data
[,1] [,2]
[1,] 0.09038971 0.34725614
[2,] -0.79411843 0.29461076
[3,] 1.23784134 -0.04727337
[4,] 1.02043216 -0.10186950
[5,] -0.16825743 -1.36044740
[6,] 1.38188126 1.22146600
[7,] -1.15402098 0.83244954
[8,] -0.62752890 1.18602495
[9,] -0.14237337 0.54557331
[10,] -1.03012840 -0.28116993