【发布时间】:2017-10-26 10:24:23
【问题描述】:
我有以下示例矩阵 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)组成的 apply 函数?
【问题讨论】:
-
您能说明一下您需要什么吗?是否要获取每个数据点并计算
(data point - column min)/(column max - column min)? -
你是对的。我想用 f 函数转换每个元素。
标签: r