利用split可以对数据进行分组
split(x, f, drop = FALSE, ...)
x 表示一个待分组的向量或者数据框
f 表示一个factor或者list,以此为规则将x分组
drop 是逻辑值,如果f中的某一个level没有用上则被弃用
例如有如下数据:
dat
Samples group
1 GSM1625995 con6h
2 GSM1625996 con6h
3 GSM1625997 con6h
4 GSM1625998 con12h
5 GSM1625999 con12h
6 GSM1626000 con12h
7 GSM1626001 con24h
8 GSM1626002 con24h
9 GSM1626003 con24h
10 GSM1626004 hm6h
11 GSM1626005 hm6h
12 GSM1626006 hm6h
13 GSM1626007 hm12h
14 GSM1626008 hm12h
15 GSM1626009 hm12h
16 GSM1626010 hm24h
17 GSM1626011 hm24h
18 GSM1626012 hm24h
按照第二列分类进行数据分组,由结果可知被划分成了六组数据
> com = split(dat,dat$group)
> com
$con12h
Samples group
4 GSM1625998 con12h
5 GSM1625999 con12h
6 GSM1626000 con12h
$con24h
Samples group
7 GSM1626001 con24h
8 GSM1626002 con24h
9 GSM1626003 con24h
$con6h
Samples group
1 GSM1625995 con6h
2 GSM1625996 con6h
3 GSM1625997 con6h
$hm12h
Samples group
13 GSM1626007 hm12h
14 GSM1626008 hm12h
15 GSM1626009 hm12h
$hm24h
Samples group
16 GSM1626010 hm24h
17 GSM1626011 hm24h
18 GSM1626012 hm24h
$hm6h
Samples group
10 GSM1626004 hm6h
11 GSM1626005 hm6h
12 GSM1626006 hm6h
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!