R矩阵应用函数

时间:2017-10-26 00:47:49

标签: r

我有以下示例矩阵x。

x <- data.frame(c1=c(1,2,3,2,1,3),
                    c2=c(4,5,6,2,3,4),
                    c3=c(7,8,9,7,1,6),
                    c4=c(4,0,9,1,5,0),
                    c5=c(3,8,0,7,3,6),
                    c6=c(2,8,5,0,5,7),
                    row.names = c("r1","r2","r3","r4","r5","r6"))

我需要将函数f应用于每个列,其中cMin是列最小值,cMax是列最大向量。

cMax <- colMaxs(mat)
cMin <- colMins(mat)

我正在尝试使用应用函数apply(mat,2,f),如下所示,但是收到警告并且结果也不正确。

f <- function(x) (x - cMin[])/(cMax - cMin)

警告: 警告信息:

1: In x - cMin[] :
  longer object length is not a multiple of shorter object length
2: In (x - cMin[])/(cMax - cMin) :
  longer object length is not a multiple of shorter object length
3: In x - cMin[] :
  longer object length is not a multiple of shorter object length
4: In (x - cMin[])/(cMax - cMin) :
  longer object length is not a multiple of shorter object length

有人可以解释如何使用包含向量的应用函数(cMin或cMax)吗?

4 个答案:

答案 0 :(得分:2)

当从矩阵中减去向量时,由于矩阵的存储机制和回收规则,向量按列对齐;因此,您可以转置matrix,使用cMincMax进行计算,然后将其转置回来:

t((t(mat) - cMin)/(cMax - cMin))

#    c1   c2    c3        c4    c5    c6
#r1 0.0 0.50 0.750 0.4444444 0.375 0.250
#r2 0.5 0.75 0.875 0.0000000 1.000 1.000
#r3 1.0 1.00 1.000 1.0000000 0.000 0.625
#r4 0.5 0.00 0.750 0.1111111 0.875 0.000
#r5 0.0 0.25 0.000 0.5555556 0.375 0.625
#r6 1.0 0.50 0.625 0.0000000 0.750 0.875

答案 1 :(得分:1)

library(magrittr)
x <- data.frame(c1=c(1,2,3,2,1,3),
                c2=c(4,5,6,2,3,4),
                c3=c(7,8,9,7,1,6),
                c4=c(4,0,9,1,5,0),
                c5=c(3,8,0,7,3,6),
                c6=c(2,8,5,0,5,7),
                row.names = c("r1","r2","r3","r4","r5","r6"))

cMin <- apply(x, MARGIN = 2, FUN = min)
cMax <- apply(x, MARGIN = 2, FUN = max)

sweep(x, MARGIN = 2, STATS = cMin, FUN = "-") %>%
  sweep(., MARGIN = 2, STATS = (cMax - cMin), FUN = "/")

    c1   c2    c3        c4    c5    c6
r1 0.0 0.50 0.750 0.4444444 0.375 0.250
r2 0.5 0.75 0.875 0.0000000 1.000 1.000
r3 1.0 1.00 1.000 1.0000000 0.000 0.625
r4 0.5 0.00 0.750 0.1111111 0.875 0.000
r5 0.0 0.25 0.000 0.5555556 0.375 0.625
r6 1.0 0.50 0.625 0.0000000 0.750 0.875

答案 2 :(得分:1)

正如我从解决方案中看到的那样,它的目标是将每列按比例缩放到0到1,线性,最小值映射到0,最大值为1。

在一行中,无需计算cMincMax

apply(x, 2, 
      function(each_col) (each_col - min(each_col))/diff(range(each_col)))

# c1   c2    c3        c4    c5    c6
# r1 0.0 0.50 0.750 0.4444444 0.375 0.250
# r2 0.5 0.75 0.875 0.0000000 1.000 1.000
# r3 1.0 1.00 1.000 1.0000000 0.000 0.625
# r4 0.5 0.00 0.750 0.1111111 0.875 0.000
# r5 0.0 0.25 0.000 0.5555556 0.375 0.625
# r6 1.0 0.50 0.625 0.0000000 0.750 0.875

答案 3 :(得分:1)

我们可以复制'cMin'和'cMax'并进行计算

(mat - cMin[col(mat)])/(cMax[col(mat)] - cMin[col(mat)])
#    c1   c2    c3        c4    c5    c6
#r1 0.0 0.50 0.750 0.4444444 0.375 0.250
#r2 0.5 0.75 0.875 0.0000000 1.000 1.000
#r3 1.0 1.00 1.000 1.0000000 0.000 0.625
#r4 0.5 0.00 0.750 0.1111111 0.875 0.000
#r5 0.0 0.25 0.000 0.5555556 0.375 0.625
#r6 1.0 0.50 0.625 0.0000000 0.750 0.875