Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
在R中,如何在单个列中排序,使一个类别上升,一个类别下降?_R_Sorting - Fatal编程技术网

在R中,如何在单个列中排序,使一个类别上升,一个类别下降?

在R中,如何在单个列中排序,使一个类别上升,一个类别下降?,r,sorting,R,Sorting,我正在生成多个不同大小和形状的实验设计。这是使用一个依赖于agricolae包的函数完成的(我在下面包括了它)。为了生成现场操作的实际数据表,我需要按行对数据帧进行排序,然后对奇数行的范围进行升序排序,对偶数行的范围进行降序排序 使用sort、order、rep和seq,我已经能够找到一个简单的解决方案。非常感谢您的任何建议 因此,数据帧将从以下内容开始: df1 <- structure(list(Block = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,

我正在生成多个不同大小和形状的实验设计。这是使用一个依赖于agricolae包的函数完成的(我在下面包括了它)。为了生成现场操作的实际数据表,我需要按行对数据帧进行排序,然后对奇数行的范围进行升序排序,对偶数行的范围进行降序排序

使用sort、order、rep和seq,我已经能够找到一个简单的解决方案。非常感谢您的任何建议

因此,数据帧将从以下内容开始:

df1 <- structure(list(Block = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), Range = c(1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), Row = c(1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 
9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L
), Plot = c(101L, 201L, 301L, 401L, 102L, 202L, 302L, 402L, 103L, 
203L, 303L, 403L, 104L, 204L, 304L, 404L, 105L, 205L, 305L, 405L, 
106L, 206L, 306L, 406L, 107L, 207L, 307L, 407L, 108L, 208L, 308L, 
408L, 109L, 209L, 309L, 409L, 110L, 210L, 310L, 410L, 111L, 211L, 
311L, 411L, 112L, 212L, 312L, 412L), Entry.Num = c(14L, 26L, 
18L, 4L, 52L, 17L, 41L, 47L, 40L, 30L, 21L, 12L, 9L, 2L, 8L, 
36L, 25L, 43L, 15L, 6L, 33L, 48L, 54L, 37L, 9L, 18L, 8L, 41L, 
48L, 28L, 7L, 47L, 54L, 38L, 46L, 23L, 19L, 1L, 3L, 27L, 36L, 
14L, 12L, 33L, 16L, 24L, 31L, 2L)), .Names = c("Block", "Range", 
"Row", "Plot", "Entry.Num"), class = "data.frame", row.names = c(NA, 
-48L))
df2 <- structure(list(Block = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), Range = c(1L, 2L, 3L, 4L, 4L, 3L, 
2L, 1L, 1L, 2L, 3L, 4L, 4L, 3L, 2L, 1L, 1L, 2L, 3L, 4L, 4L, 3L, 
2L, 1L, 1L, 2L, 3L, 4L, 4L, 3L, 2L, 1L, 1L, 2L, 3L, 4L, 4L, 3L, 
2L, 1L, 1L, 2L, 3L, 4L, 4L, 3L, 2L, 1L), Row = c(1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 
9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L
), Plot = c(101L, 201L, 301L, 401L, 402L, 302L, 202L, 102L, 103L, 
203L, 303L, 403L, 404L, 304L, 204L, 104L, 105L, 205L, 305L, 405L, 
406L, 306L, 206L, 106L, 107L, 207L, 307L, 407L, 408L, 308L, 208L, 
108L, 109L, 209L, 309L, 409L, 410L, 310L, 210L, 110L, 111L, 211L, 
311L, 411L, 412L, 312L, 212L, 112L), Entry.Num = c(14L, 26L, 
18L, 4L, 47L, 41L, 17L, 52L, 40L, 30L, 21L, 12L, 36L, 8L, 2L, 
9L, 25L, 43L, 15L, 6L, 37L, 54L, 48L, 33L, 9L, 18L, 8L, 41L, 
47L, 7L, 28L, 48L, 54L, 38L, 46L, 23L, 27L, 3L, 1L, 19L, 36L, 
14L, 12L, 33L, 2L, 31L, 24L, 16L)), .Names = c("Block", "Range", 
"Row", "Plot", "Entry.Num"), class = "data.frame", row.names = c(NA, 
-48L))

df1订单的魔力等待着您:

df1[order(df1$Row, c(-1,1)[df1$Row %% 2 + 1] * df1$Range ),]
基本上,这是按
排序,然后按
范围
,如果是偶数,则乘以
-1
<代码>x%%2
可用于检查奇数/偶数状态

all.equal(
  df1[order(df1$Row, c(-1,1)[df1$Row %% 2 + 1] * df1$Range ),],
  df2,
  check.attributes=FALSE
)
#[1] TRUE

订单的魔力等待着你:

df1[order(df1$Row, c(-1,1)[df1$Row %% 2 + 1] * df1$Range ),]
基本上,这是按
排序,然后按
范围
,如果是偶数,则乘以
-1
<代码>x%%2
可用于检查奇数/偶数状态

all.equal(
  df1[order(df1$Row, c(-1,1)[df1$Row %% 2 + 1] * df1$Range ),],
  df2,
  check.attributes=FALSE
)
#[1] TRUE

事情总是简单的。谢谢。总是简单的事情。谢谢