天昊生信团自4月份以来,陆续推出一系列的r基础教程,很多老师也展开r的学习。目前收到部分老师反馈如何对一组数据进行列求和。之所以提出这个问题,是因为excel数据中样品是无规则排列,即使行排序也不能将同一组的样品靠在一起;或者样品数量较多,分组较多,需要多次输入求和函数。这里,我们提供了一个r的统计教程,帮您解决这方面的问题。
一读取数据
1.读取绝对丰度数据
in [1]:
df <- read.table('genus.taxon.abundance.xls',header = t, row.names = 1, sep = 't',check.names = false,quote = '',fill = t)
head(df,10)out[1]:
c-1 c-2 c-3 c-4 c-5 c-6 c-7 m-1 m-2 m-3 ... m-5 m-6 m-7 m-8 abundance superkingdom phylum class order family
no_rank 32729 43885 36532 29335 21815 26879 41869 31030 29862 33787 ... 43164 30980 21853 21792 471140 - - - - -
lactobacillus 24438 24449 3760 18204 28795 4537 12831 24450 17354 26211 ... 30850 19648 21548 18263 291188 bacteria firmicutes bacilli lactobacillales lactobacillaceae
unassigned 25317 21725 13405 19347 14697 16676 19162 15819 22083 21072 ... 12032 7750 33000 26360 279560 bacteria bacteroidetes bacteroidia bacteroidales porphyromonadaceae
alistipes 5720 6196 18812 14525 19759 21996 10696 4411 5670 3663 ... 5744 6580 5415 5571 146280 bacteria bacteroidetes bacteroidia bacteroidales rikenellaceae
parabacteroides 6092 2683 8164 10246 10619 8742 7885 4493 12209 2423 ... 5628 4943 6382 11633 118324 bacteria bacteroidetes bacteroidia bacteroidales porphyromonadaceae
bacteroides 9803 3257 7719 5919 8039 6959 3502 6514 8694 3840 ... 7839 4368 11986 15711 116935 bacteria bacteroidetes bacteroidia bacteroidales bacteroidaceae
barnesiella 2982 1414 2341 9184 3707 6875 3812 3183 2773 5250 ... 3128 6584 3764 6680 64267 bacteria bacteroidetes bacteroidia bacteroidales porphyromonadaceae
eisenbergiella 680 2283 4357 1743 1251 6988 3731 1832 2204 1598 ... 1193 649 3466 4671 45890 bacteria firmicutes clostridia clostridiales lachnospiraceae
oscillibacter 653 714 4704 1569 2210 4680 4303 2748 4912 800 ... 2424 729 1287 2486 37738 bacteria firmicutes clostridia clostridiales ruminococcaceae
turicibacter 1373 2366 20 1405 56 6 2091 5060 9 9229 ... 1 1 4360 1693 27673 bacteria firmicutes erysipelotrichia erysipelotrichales erysipelotrichaceae
in [2]:
info = read.table('sample_groups.xls',header = f,col.names = c('sample','group'))head(info)out[2]:
sample group
c-1 control
c-2 control
c-3 control
c-4 control
c-5 control
c-6 control
in [3]:
info$sampleout[3]:
c-1 c-2 c-3 c-4 c-5 c-6 c-7 m-1 m-2 m-3 m-4 m-5 m-6 m-7 m-8
in [4]:
df = df[as.character(info$sample)]head(df)out[4]:
c-1 c-2 c-3 c-4 c-5 c-6 c-7 m-1 m-2 m-3 m-4 m-5 m-6 m-7 m-8
no_rank 32729 43885 36532 29335 21815 26879 41869 31030 29862 33787 25628 43164 30980 21853 21792
lactobacillus 24438 24449 3760 18204 28795 4537 12831 24450 17354 26211 15850 30850 19648 21548 18263
unassigned 25317 21725 13405 19347 14697 16676 19162 15819 22083 21072 11115 12032 7750 33000 26360
alistipes 5720 6196 18812 14525 19759 21996 10696 4411 5670 3663 11522 5744 6580 5415 5571
parabacteroides 6092 2683 8164 10246 10619 8742 7885 4493 12209 2423 16182 5628 4943 6382 11633
bacteroides 9803 3257 7719 5919 8039 6959 3502 6514 8694 3840 12785 7839 4368 11986 15711
in [5]:
info$groupout[5]:
control control control control control control control nnk-bap nnk-bap nnk-bap nnk-bap nnk-bap nnk-bap nnk-bap nnk-bap分组信息发现,仅有2个分组,组名分别是control、nnk-bap
二计算该组的丰度之和、均值、方差、标准差
查看control组包含哪些样品
in [6]:
info[info$group=='control',1]
out[6]:
c-1 c-2 c-3 c-4 c-5 c-6 c-7
apply函数计算control组的和、均值、方差、标准差
in [7]:
df['control_sum'] = apply(df[as.character(info[info$group=='control',1])],1,sum)
df['control_mean'] = apply(df[as.character(info[info$group=='control',1])],1,mean)
df['control_var'] = apply(df[as.character(info[info$group=='control',1])],1,var)
df['control_sd'] = apply(df[as.character(info[info$group=='control',1])],1,sd)
head(df)
out[7]:
c-1 c-2 c-3 c-4 c-5 c-6 c-7 m-1 m-2 m-3 m-4 m-5 m-6 m-7 m-8 control_sum control_mean control_var control_sd
no_rank 32729 43885 36532 29335 21815 26879 41869 31030 29862 33787 25628 43164 30980 21853 21792 233044 33292.000 64182849 8011.420
lactobacillus 24438 24449 3760 18204 28795 4537 12831 24450 17354 26211 15850 30850 19648 21548 18263 117014 16716.286 99804027 9990.197
unassigned 25317 21725 13405 19347 14697 16676 19162 15819 22083 21072 11115 12032 7750 33000 26360 130329 18618.429 16946400 4116.601
alistipes 5720 6196 18812 14525 19759 21996 10696 4411 5670 3663 11522 5744 6580 5415 5571 97704 13957.714 43482964 6594.161
parabacteroides 6092 2683 8164 10246 10619 8742 7885 4493 12209 2423 16182 5628 4943 6382 11633 54431 7775.857 7342272 2709.663
bacteroides 9803 3257 7719 5919 8039 6959 3502 6514 8694 3840 12785 7839 4368 11986 15711 45198 6456.857 5800759 2408.477
apply函数计算nnk-bap组的和、均值、方差、标准差
in [8]:
df['nnk-bap_sum'] = apply(df[as.character(info[info$group=='nnk-bap',1])],1,sum)
df['nnk-bap_mean'] = apply(df[as.character(info[info$group=='nnk-bap',1])],1,mean)
df['nnk-bap_var'] = apply(df[as.character(info[info$group=='nnk-bap',1])],1,var)
df['nnk-bap_sd'] = apply(df[as.character(info[info$group=='nnk-bap',1])],1,sd)
head(df)
out[8]:
c-1 c-2 c-3 c-4 c-5 c-6 c-7 m-1 m-2 m-3 ... m-7 m-8 control_sum control_mean control_var control_sd nnk-bap_sum nnk-bap_mean nnk-bap_var nnk-bap_sd
no_rank 32729 43885 36532 29335 21815 26879 41869 31030 29862 33787 ... 21853 21792 233044 33292.000 64182849 8011.420 238096 29762.000 48868388 6990.593
lactobacillus 24438 24449 3760 18204 28795 4537 12831 24450 17354 26211 ... 21548 18263 117014 16716.286 99804027 9990.197 174174 21771.750 25821419 5081.478
unassigned 25317 21725 13405 19347 14697 16676 19162 15819 22083 21072 ... 33000 26360 130329 18618.429 16946400 4116.601 149231 18653.875 72916680 8539.126
alistipes 5720 6196 18812 14525 19759 21996 10696 4411 5670 3663 ... 5415 5571 97704 13957.714 43482964 6594.161 48576 6072.000 5639229 2374.706
parabacteroides 6092 2683 8164 10246 10619 8742 7885 4493 12209 2423 ... 6382 11633 54431 7775.857 7342272 2709.663 63893 7986.625 22692800 4763.696
bacteroides 9803 3257 7719 5919 8039 6959 3502 6514 8694 3840 ... 11986 15711 45198 6456.857 5800759 2408.477 71737 8967.125 17710582 4208.394
三按列计算相对丰度
in [9]:
df_relative = apply(df,2,function(d) d/sum(d))
head(df_relative)
out[9]:
c-1 c-2 c-3 c-4 c-5 c-6 c-7 m-1 m-2 m-3 ... m-7 m-8 control_sum control_mean control_var control_sd nnk-bap_sum nnk-bap_mean nnk-bap_var nnk-bap_sd
no_rank 0.13115102 0.17585513 0.14639033 0.11755065 0.08741665 0.10770901 0.16777666 0.12434282 0.11966244 0.135390620 ... 0.08756892 0.08732449 0.13340707 0.13340707 0.24318010 0.14420668 0.11926172 0.11926172 0.19457417 0.10579967
lactobacillus 0.09792749 0.09797157 0.01506700 0.07294672 0.11538677 0.01818058 0.05141614 0.09797557 0.06954062 0.105032218 ... 0.08634673 0.07318314 0.06698518 0.06698518 0.37814391 0.17982493 0.08724334 0.08724334 0.10281045 0.07690602
unassigned 0.10144980 0.08705600 0.05371626 0.07752693 0.05889354 0.06682375 0.07678560 0.06338959 0.08849058 0.084439315 ... 0.13223697 0.10562929 0.07460741 0.07460741 0.06420761 0.07409938 0.07474945 0.07474945 0.29032475 0.12923607
alistipes 0.02292107 0.02482849 0.07538309 0.05820430 0.07917789 0.08814195 0.04286081 0.01767567 0.02272072 0.014678304 ... 0.02169888 0.02232400 0.05593109 0.05593109 0.16475105 0.11869583 0.02433160 0.02433160 0.02245313 0.03594017
parabacteroides 0.02441175 0.01075127 0.03271462 0.04105758 0.04255225 0.03503078 0.03159662 0.01800426 0.04892367 0.009709399 ... 0.02557383 0.04661554 0.03115927 0.03115927 0.02781887 0.04877431 0.03200385 0.03200385 0.09035356 0.07209653
bacteroides 0.03928239 0.01305139 0.03093143 0.02371850 0.03221373 0.02788597 0.01403315 0.02610278 0.03483843 0.015387575 ... 0.04803007 0.06295682 0.02587379 0.02587379 0.02197829 0.04335292 0.03593289 0.03593289 0.07051638 0.06369227
四写入文件进行保存
in [10]:
write.csv(df_relative,'genus.taxon.relativeabundance.cvs')
往期相关链接:
1、r基础篇
;
;
;
;
;
;
;
2、r进阶
;
;
;
3、数据提交
;
;
;
;
4、表达谱分析
;
5、医学数据分析
;
;
;
如果您对本文案介绍的方法或代码有疑问,
请扫码添加qq群沟通
【本群将为大家提供】
分享生信分析方案
提供数据素材及分析软件支持
定期开展生信分析线上讲座
qq号:1040471849