Normalize rows of a matrix within range 0 and 1

后端 未结 3 2054
孤城傲影
孤城傲影 2020-12-30 02:28

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\",

3条回答
  •  一个人的身影
    2020-12-30 02:46

    You could use the apply with rescale as the following:

    apply(mydata, 1, rescale)
    

    where the second argument 1 tells apply to work with rows.

    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:

    apply(mydata, 1, rescale, to=c(1,2))
    

    Dependecy:

    if(!require(scales)){
        install.packages("scales", dependencies=TRUE)
        library(scales)
    }
    

提交回复
热议问题