如何将数据帧值重新缩放为 0 和 1?

问题描述 投票:0回答:1

我想将我的数据框

protein
重新缩放到 0 到 1 之间。

protein <- as.numeric(unlist(protein))    
protein <- apply(protein, MARGIN = 2, FUN = function(X) (X - min(X))/diff(range(X)))

回溯:

Error in apply(protein, MARGIN = 2, FUN = function(X) (X - min(X))/diff(range(X))) : 
  dim(X) must have a positive length

数据:

> dput(protein[1:10,1:10])
structure(list(TCGA.A3.3357.01 = c("0.43216667125", "0.27655395325", 
"-0.05052052725", "-0.13792455525", "0.07409333175", "-0.47918122425", 
"-0.0175397277500001", "-0.31555082025", "0.17598078475", "-0.32440180325"
), TCGA.A3.3367.01 = c("0.22529132975", "-0.12950190925", "0.00483883425000009", 
"0.13180277925", "-0.09403284175", "-0.32915906875", "0.16454402775", 
"0.0673332472499999", "0.0282689282499999", "-0.57593626275"), 
    TCGA.A3.3387.01 = c("0.0428932150000005", "0.0548635820000001", 
    "0.0339660955000001", "-0.0440775865", "-0.2434882025", "-0.1171357375", 
    "-0.500200476", "0.4330998585", "-0.0195380545000001", "0.0573784155"
    ), TCGA.B0.4698.01 = c("0.502301219", "0.327746334", "2.5017964265", 
    "0.8452782975", "0.0452485464999999", "0.6489117815", "0.610451403", 
    "-0.5377816785", "0.3412347885", "1.1108757225"), TCGA.B0.4710.01 = c("0.342431330750001", 
    "0.33447894175", "0.30921895025", "0.29504738925", "-0.26837205975", 
    "0.90950231825", "-0.67599687525", "-0.34704194275", "0.12366059425", 
    "0.0922109122500001"), TCGA.B0.4810.01 = c("-0.1528553115", 
    "-0.0890113614999999", "0.110224878", "0.531443884", "0.705128991", 
    "1.576618035", "0.6941767135", "-0.013302181", "-0.203134214", 
    "0.528934355"), TCGA.B0.4811.01 = c("0.0564247582500002", 
    "-0.0312831207499999", "-0.11710900625", "-0.00937909725000008", 
    "0.10141934375", "0.63522999475", "-0.11573650675", "0.36754486275", 
    "-0.10905751725", "0.51649620875"), TCGA.B0.4815.01 = c("0.0711154310000005", 
    "-0.0181620079999999", "-0.00635163549999995", "-0.2597877095", 
    "-0.0626275074999999", "0.1845338715", "0.117613381", "0.2185215415", 
    "-0.4099665705", "0.1484769175"), TCGA.B0.4818.01 = c("-0.0697927272499997", 
    "0.12253850775", "0.39577983525", "0.40226443825", "0.0962845262499997", 
    "0.46560829625", "0.29552204975", "-0.23290107475", "0.29492263125", 
    "0.27064606525"), TCGA.B0.4821.01 = c("0.13600020325", "0.15874590925", 
    "0.47381080675", "0.0716422437499998", "-0.29596241525", 
    "0.23818164575", "0.17422681825", "-0.52034638725", "0.60918152875", 
    "-0.17272622525")), row.names = c("14-3-3_beta", "14-3-3_epsilon", 
"14-3-3_zeta", "4E-BP1", "4E-BP1_pS65", "4E-BP1_pT37_T46", "4E-BP1_pT70", 
"53BP1", "A-Raf_pS299", "ACC1"), class = "data.frame")
r
1个回答
0
投票

Based on your original

protein
data, you could use

apply(protein, 2, \(x) {
  x <- as.numeric(x) 
  (x - min(x)) / diff(range(x))
})

使用

unlist()
如您的第一个代码块所示,没有任何意义(至少对我而言),因为它将 data.frame 更改为向量。

相关问题
热门问答
最新问题