在R中按特定顺序排序数据

时间:2018-05-30 13:48:25

标签: r sorting

假设我有以下data.frame;

/*  SECTIONS  ============================================================================= */

.section {
	clear: both;
	padding: 0px;
	margin: 0px;
}

/*  GROUPING  ============================================================================= */

.row {
    zoom: 1; /* For IE 6/7 (trigger hasLayout) */
}
.row:before,
.row:after {
    content:"";
    display: table;
}
.row:after {
    clear:both;
}

/*  GRID COLUMN SETUP   ==================================================================== */

.col {
	display: block;
	float:left;
	margin: 1% 0 1% 1.6%;
}

.col:first-child { margin-left: 0; } /* all browsers except IE6 and lower */


/*  REMOVE MARGINS AS ALL GO FULL WIDTH AT 480 PIXELS */

@media only screen and (max-width: 480px) {
	.col { 
		/*margin: 1% 0 1% 0%;*/
        margin: 0;
	}
}

/*  GRID OF TWO   ============================================================================= */


.span-2-of-2 {
	width: 100%;
}

.span-1-of-2 {
	width: 49.2%;
}

/*  GO FULL WIDTH AT LESS THAN 480 PIXELS */

@media only screen and (max-width: 480px) {
	.span-2-of-2 {
		width: 100%; 
	}
	.span-1-of-2 {
		width: 100%; 
	}
}

/*  GRID OF THREE  ============================================================================= */

.span-3-of-3 {
    width: 100%;
}

.span-2-of-3 {
    width: 66.13%;
}

.span-1-of-3 {
    width: 32.26%;
}

/*  GO FULL WIDTH AT LESS THAN 480 PIXELS */

@media only screen and (max-width: 480px) {
	.span-3-of-3 {
		width: 100%; 
	}
	.span-2-of-3 {
		width: 100%; 
	}
    .span-1-of-3 {
        width: 100%;
    }
}

/*  GRID OF FOUR  ============================================================================= */

.span-4-of-4 {
    width: 100%;
}
.span-3-of-4 {
    width: 74.6%;
}
.span-2-of-4 {
    width: 49.2%;
}
.span-1-of-4 {
    width: 23.8%;
}

/*  GO FULL WIDTH AT LESS THAN 480 PIXELS */

@media only screen and (max-width: 480px) {
	.span-4-of-4 {
		width: 100%; 
	}
	.span-3-of-4 {
		width: 100%; 
	}
    .span-2-of-4 {
        width: 100%;
    }
    .span-1-of-4 {
        width: 100%;
    }
}

我怎样才能这样订购;

NUM   status  name
 1       +       A
 2       +       A
 3       +       B 
 4       -       A
 5       -       A 
 6       +       C 
 7       -       C 
 8       +       A
 9       -       B
 10      -       A 

我想先按名称订购。然后按状态。但我需要以这种模式订购状态; +, - ,+, - 提前谢谢。

4 个答案:

答案 0 :(得分:2)

您可以在R:

中执行此操作
df <- df[with(df,order(status,name)),
         ][c(nrow(df)/2,0)+rep(1:(nrow(df)/2),each=2),]
#     NUM status name
# 1    1      +    A
# 4    4      -    A
# 2    2      +    A
# 5    5      -    A
# 8    8      +    A
# 10  10      -    A
# 3    3      +    B
# 9    9      -    B
# 6    6      +    C
# 7    7      -    C

答案 1 :(得分:2)

我们可以row_number使用dplyr group_by创建帮助键以实现特定订单

df %>% 
  group_by(name,status) %>%
  mutate(Helpkey=row_number()) %>%
  arrange(name,Helpkey) %>%
  select(-Helpkey)

# A tibble: 10 x 3
# Groups:   name, status [6]
     NUM status  name
   <int>  <chr> <chr>
 1     1      +     A
 2     4      -     A
 3     2      +     A
 4     5      -     A
 5     8      +     A
 6    10      -     A
 7     3      +     B
 8     9      -     B
 9     6      +     C
10     7      -     C

答案 2 :(得分:1)

您可以按name排序,然后按给定name, status对发生的次数排序。

library(data.table)
setDT(df)

df[order(name, rowid(name, status))]

#     NUM status name
#  1:   1      +    A
#  2:   4      -    A
#  3:   2      +    A
#  4:   5      -    A
#  5:   8      +    A
#  6:  10      -    A
#  7:   3      +    B
#  8:   9      -    B
#  9:   6      +    C
# 10:   7      -    C

使用的数据:

df <- fread("
NUM   status  name
 1       +       A
 2       +       A
 3       +       B 
 4       -       A
 5       -       A 
 6       +       C 
 7       -       C 
 8       +       A
 9       -       B
 10      -       A 
")

答案 3 :(得分:0)

data.table方法:

library(data.table)

csv <- "
NUM   status  name
 1       +       A
 2       +       A
 3       +       B 
 4       -       A
 5       -       A 
 6       +       C 
 7       -       C 
 8       +       A
 9       -       B
 10      -       A
"

data <- fread(csv)
data[, id := seq_len(.N), by = .(name, status)]
data[order(name, id), .(NUM, status, name)]

    NUM status name
 1:   1      +    A
 2:   4      -    A
 3:   2      +    A
 4:   5      -    A
 5:   8      +    A
 6:  10      -    A
 7:   3      +    B
 8:   9      -    B
 9:   6      +    C
10:   7      -    C