I am trying to normalize all rows of my matrix data at once within range 0 and 1. But I don't know how to do it.. For example, I want to normalize each "obs1", "obs2", "obs3". Thus, minimum, maximum, and sum of each "obs1", "obs2", "obs3" will be used. My data format is,
我试图在0和1范围内一次归一化我的矩阵数据的所有行。但我不知道该怎么做..例如,我想规范化每个“obs1”,“obs2”,“obs3” 。因此,将使用每个“obs1”,“obs2”,“obs3”的最小值,最大值和总和。我的数据格式是,
`mydata
`MYDATA
a b c d e
obs1 8.15609 11.5379 11.1401 8.95186 7.95722
obs2 339.89800 856.3470 691.3490 590.28600 543.67200
obs3 2.12776 46.4561 136.8860 118.09100 119.86400
`
`
Also, When I searched to perform this, people used "function()". When/for what does this used?
此外,当我搜索执行此操作时,人们使用“function()”。何时使用?
Thank you very much for your help in advance! :)
非常感谢您的帮助! :)
3 个解决方案
#1
22
To normalize for each row, you can use apply
and then subtract the minimum from each column and divide by the difference between maximum and minimum:
要标准化每一行,您可以使用apply然后从每列中减去最小值并除以最大值和最小值之间的差值:
t(apply(mydata, 1, function(x)(x-min(x))/(max(x)-min(x))))
gives you
给你
a b c d e
obs1 0.05553973 1.0000000 0.8889038 0.2777796 0.0000000
obs2 0.00000000 1.0000000 0.6805144 0.4848262 0.3945675
obs3 0.00000000 0.3289472 1.0000000 0.8605280 0.8736849
What happens is that you apply the function
会发生什么是您应用该功能
function(x){
(x-min(x))/(max(x)-min(x))
}
to each row of your data frame.
到数据框的每一行。
#2
0
for(i in 2:length(mydata[1,])){
mydata[,i] <- prop.table(mydata[,i])
}
Normalized matrix will be updated in mydata
归一化矩阵将在mydata中更新
#3
0
You could use the apply
with rescale
as the following:
您可以使用apply with rescale,如下所示:
apply(mydata, 1, rescale)
申请(mydata,1,rescale)
where the second argument 1
tells apply to work with rows.
其中第二个参数1告诉应用于行。
The default range is [0, 1] but a custom range can be specified with the to
argument that will be forwarded to the rescale
function:
默认范围是[0,1],但可以使用将转发到rescale函数的to参数指定自定义范围:
apply(mydata, 1, rescale, to=c(1,2))
apply(mydata,1,rescale,to = c(1,2))
Dependecy:
Dependecy:
if(!require(scales)){ install.packages("scales", dependencies=TRUE) library(scales) }
if(!require(scales)){install.packages(“scales”,dependencies = TRUE)library(scales)}
#1
22
To normalize for each row, you can use apply
and then subtract the minimum from each column and divide by the difference between maximum and minimum:
要标准化每一行,您可以使用apply然后从每列中减去最小值并除以最大值和最小值之间的差值:
t(apply(mydata, 1, function(x)(x-min(x))/(max(x)-min(x))))
gives you
给你
a b c d e
obs1 0.05553973 1.0000000 0.8889038 0.2777796 0.0000000
obs2 0.00000000 1.0000000 0.6805144 0.4848262 0.3945675
obs3 0.00000000 0.3289472 1.0000000 0.8605280 0.8736849
What happens is that you apply the function
会发生什么是您应用该功能
function(x){
(x-min(x))/(max(x)-min(x))
}
to each row of your data frame.
到数据框的每一行。
#2
0
for(i in 2:length(mydata[1,])){
mydata[,i] <- prop.table(mydata[,i])
}
Normalized matrix will be updated in mydata
归一化矩阵将在mydata中更新
#3
0
You could use the apply
with rescale
as the following:
您可以使用apply with rescale,如下所示:
apply(mydata, 1, rescale)
申请(mydata,1,rescale)
where the second argument 1
tells apply to work with rows.
其中第二个参数1告诉应用于行。
The default range is [0, 1] but a custom range can be specified with the to
argument that will be forwarded to the rescale
function:
默认范围是[0,1],但可以使用将转发到rescale函数的to参数指定自定义范围:
apply(mydata, 1, rescale, to=c(1,2))
apply(mydata,1,rescale,to = c(1,2))
Dependecy:
Dependecy:
if(!require(scales)){ install.packages("scales", dependencies=TRUE) library(scales) }
if(!require(scales)){install.packages(“scales”,dependencies = TRUE)library(scales)}