Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用order在R中进行2d排序_R - Fatal编程技术网

如何使用order在R中进行2d排序

如何使用order在R中进行2d排序,r,R,这是一个令人尴尬的简单问题,但我似乎无法让order()服从我的意愿 我有一些数据对(x,y),我只想在x上排序,然后在y上排序。例如 2, 1 3, 2 1, 3 重新排序为: 1, 3 2, 1 3, 2 在R中,order()返回行索引的向量。在您的示例中: > order(data$x) [1] 3 1 2 df <- data.frame(x=c(2,3,1), y=c(1,2,3) ) df[order(df$x), ] 您可以将其解释为“最低值在第3行,第二低

这是一个令人尴尬的简单问题,但我似乎无法让order()服从我的意愿

我有一些数据对(x,y),我只想在x上排序,然后在y上排序。例如

2, 1
3, 2
1, 3
重新排序为:

1, 3
2, 1
3, 2
在R中,
order()
返回行索引的向量。在您的示例中:

> order(data$x)
[1] 3 1 2
df <- data.frame(x=c(2,3,1), y=c(1,2,3) )

df[order(df$x), ]
您可以将其解释为“最低值在第3行,第二低值在第1行”,依此类推。重要的是,它不会以任何方式更改数据帧。要使用
order()
获得按x排序的数据帧,只需使用:

> data[order(data$x),]
  x y
3 1 3
1 2 1
2 3 2
在R中,order(vector)将按所需顺序为您提供标记。使用这些标记,您可以对两个向量重新排序。 以下是用于示例的命令:

> order(data$x)
[1] 3 1 2
df <- data.frame(x=c(2,3,1), y=c(1,2,3) )

df[order(df$x), ]

df甜美、美好、简单。非常感谢。(是的,我的意思是R,我猜你的意思是不改变数据帧。)是的,的确如此。我修正了我的答案+1对于另一个光环的粉丝。