R语言做生信生信工具R数据读取 清理

R语言之数据标准化方法大全

2019-04-21  本文已影响53人  Oodelay

decostand 是群落生态学中常用的工具包,提供了很多主流且高效的数据标准化方法。

基本语法

decostand (x,method, MARGIN, range.global,logbase = 2, na.rm = FALSE,...)

标准化,和转化相反,是求相对值,旨在降低数据之间因量级、单位等差异而带来的数据异质性。

示例

(dat = matrix(sample(seq(100)),nrow = 20,dimnames = list(paste0('OTU_',seq(20)),paste0('smp',seq(5)))))
library('vegan')
library('dplyr')
decostand(dat,'total') %>% rowSums()
decostand(dat,'total',2) %>% colSums()

t(t(df)/colSums(df))
dat/matrix(rep(colSums(dat),nrow(dat)), nrow = nrow(dat), byrow = T)
sweep(dat,2,colSums(dat),`/`)
scale(dat, center=FALSE, scale=colSums(dat))
decostand(dat,'max') %>% summary()
decostand(dat,'max',1) %>% summary()

decostand(dat,'frequency') %>% colMeans()
decostand(dat,'frequency',1) %>% rowMeans()

decostand(dat,'normalize') %>% apply(1,function(x) sum(x^2))
decostand(dat,'normalize',2) %>% apply(2,function(x) sum(x^2))

'normalize'
decostand(dat,'range') %>% summary()  #apply(dat, 2, function (x) (max(x)-x)/(max(x)-min(x)))
decostand(dat,'range',1) %>% summary() #apply(dat, 1, function (x) (max(x)-x)/(max(x)-min(x)))

decostand(dat, 'standardize') %>% summary()
decostand(dat, 'standardize',1) %>% summary()

decostand(dat,'chi.square')
# (dat / rowSums(dat)) %*% diag(1/sqrt(colSums(dat))) * sqrt(sum(dat))

decostand(dat,'log') %>% summary()
'log'
decostand(dat,'rank',2)
decostand(dat,'pa') %>% summary()
上一篇 下一篇

猜你喜欢

热点阅读