R:如何按二进制类别对数据帧行进行分组



我想对bipolar数据帧重新排序,使带有1/2二进制分类变量的Indication列显示为前几行,然后是带有0/1的行。

library(dplyr)
setDT(dat); setDT(ann)
bipolar <- ann %>%
select(FID=Database_ID,Indication=Profile) %>% 
mutate(Indication=recode(Indication,"Unaffected control"="0/1", "BP"="1/2")) %>% 
inner_join(dat, ., by="FID")
bipolar %>% group_by(Indication) %>% tally() %>% replace(., is.na(.),0)

所需输出示例:

>C2<2th>指示[/tr>-0.028517201/2-0.042206101/20.00310475<2>-0.013510500/10/1-0.028517201/2
FIDIIDSOL2C1
AC13100.00450319
AC14100.00394058
AC18100.03357880
AC1510-0.03165270
AC19100.004538140.01607500
AC13100.00450319

logical条件上的排序将FALSE排序在TRUE之前(可能是因为FALSE类似于0,而TRUE类似于1(。

bipolar[order(!Indication == "1/2"),]
#        FID   IID   SOL          C1          C2 Indication
#     <char> <int> <int>       <num>       <num>     <char>
#  1:   AC13     1     0 -0.02851720  0.00450319        1/2
#  2:   AC14     1     0 -0.04220610  0.00394058        1/2
#  3:   AC18     1     0  0.03357880  0.00310475        1/2
#  4:    AC1     1     0 -0.01968050  0.01086060        1/2
#  5:   AC20     1     0  0.00871406  0.01950240        1/2
#  6:   AC21     1     0 -0.03599980 -0.01099320        1/2
#  7:   AC23     1     0  0.00776039  0.02644000        1/2
#  8:    AC2     1     0 -0.02925640 -0.03535710        1/2
#  9:   AC32     1     0 -0.01045660  0.02786490        1/2
# 10:   AC34     1     0 -0.01162300  0.01986990        1/2
# ---                                                      
# 85:   DE22     1     0 -0.02442820  0.01837440        0/1
# 86:   DE23     1     0 -0.02459090 -0.02554800        0/1
# 87:   DE37     1     0  0.00868522 -0.02671490        0/1
# 88:   DE38     1     0 -0.03472330  0.00712551        0/1
# 89:   DE39     1     0  0.00191488 -0.01151990        0/1
# 90:   DE46     1     0  0.00826277 -0.01826540        0/1
# 91:   DE47     1     0 -0.01241040 -0.02630840        0/1
# 92:    DE4     1     0 -0.01362810  0.00126466        0/1
# 93:   DE50     1     0 -0.02365100  0.03978020        0/1
# 94:   DE59     1     0 -0.01987270 -0.01633340        0/1

据我所知,您正试图按Indication对数据集进行反向排序。有很多方法可以做到这一点,其中之一就是:

dplyr::arrange(bipolar, desc(Indication))

我们可以使用arrange

library(dplyr)
bipolar %>%
arrange( Indication != "1/2")

-输出

FID   IID   SOL           C1           C2 Indication
<char> <int> <int>        <num>        <num>     <char>
1:   AC13     1     0 -0.028517200  0.004503190        1/2
2:   AC14     1     0 -0.042206100  0.003940580        1/2
3:   AC18     1     0  0.033578800  0.003104750        1/2
4:    AC1     1     0 -0.019680500  0.010860600        1/2
5:   AC20     1     0  0.008714060  0.019502400        1/2
6:   AC21     1     0 -0.035999800 -0.010993200        1/2
7:   AC23     1     0  0.007760390  0.026440000        1/2
8:    AC2     1     0 -0.029256400 -0.035357100        1/2
9:   AC32     1     0 -0.010456600  0.027864900        1/2
10:   AC34     1     0 -0.011623000  0.019869900        1/2
11:   AC36     1     0  0.003272810  0.006369330        1/2
12:   AC42     1     0 -0.005648140  0.024725500        1/2
13:   AC46     1     0  0.002091830  0.004186580        1/2
14:   AC48     1     0 -0.024517800 -0.010631700        1/2
15:   AC50     1     0 -0.001292300 -0.049019300        1/2
...

最新更新