R语言 在0和1之间缩放数据

wj8zmpe1  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(141)

我想缩放我的dataframe protein,使值在0和1之间。

protein <- (protein - min(protein)) / (max(protein) - min(protein))

追溯:

Error in FUN(X[[i]], ...) : 
  only defined on a data frame with all numeric-alike variables

我尝试了unlist,但错误仍然存在。

> typeof(protein)
[1] "list"

数据:

> dput(protein)
structure(list(TCGA.A3.3357.01 = c("0.43216667125", "0.27655395325", 
"-0.05052052725"), TCGA.A3.3367.01 = c("0.22529132975", "-0.12950190925", 
"0.00483883425000009"), TCGA.A3.3387.01 = c("0.0428932150000005", 
"0.0548635820000001", "0.0339660955000001")), row.names = c("14-3-3_beta", 
"14-3-3_epsilon", "14-3-3_zeta"), class = "data.frame")
mdfafbf1

mdfafbf11#

将列转换为numeric并使用apply

df_out <- apply(protein, 2, \(x) {x <- as.numeric(x); (x - min(x))/(max(x) - min(x))}) |> as.data.frame()
rownames(df_out) <- rownames(protein)

protein
               TCGA.A3.3357.01     TCGA.A3.3367.01    TCGA.A3.3387.01
14-3-3_beta      0.43216667125       0.22529132975 0.0428932150000005
14-3-3_epsilon   0.27655395325      -0.12950190925 0.0548635820000001
14-3-3_zeta     -0.05052052725 0.00483883425000009 0.0339660955000001

数据

protein <- structure(list(TCGA.A3.3357.01 = c("0.43216667125", "0.27655395325", 
"-0.05052052725"), TCGA.A3.3367.01 = c("0.22529132975", "-0.12950190925", 
"0.00483883425000009"), TCGA.A3.3387.01 = c("0.0428932150000005", 
"0.0548635820000001", "0.0339660955000001")), row.names = c("14-3-3_beta", 
"14-3-3_epsilon", "14-3-3_zeta"), class = "data.frame")
polhcujo

polhcujo2#

使用{dplyr}的方法:

library(dplyr)

looks_numeric <- \(xs) !all(is.na(as.double(xs)))
scale_to_unit <- \(xs) (xs-min(xs, na.rm = TRUE))/
                        diff(range(xs, na.rm = TRUE)
                        )

protein |>
   mutate(across(where(looks_numeric), ~ as.numeric(.x))) |>
   mutate(across(where(is.numeric), ~ scale_to_unit(.x)))

edit正如Julien所指出的,across-mutate都可以集总:

protein |>
mutate(across(where(looks_numeric), ~ as.numeric(.x)),
       across(where(is.numeric), ~ scale_to_unit(.x))
       )
##                TCGA.A3.3357.01 TCGA.A3.3367.01 TCGA.A3.3387.01
## 14-3-3_beta          1.0000000       1.0000000       0.4271863
## 14-3-3_epsilon       0.6776117       0.0000000       1.0000000
## 14-3-3_zeta          0.0000000       0.3786452       0.0000000

相关问题