[R] Mapping data onto score
jim holtman
jholtman at gmail.com
Wed Jul 16 21:14:19 CEST 2008
Try this and see how fast it runs:
# x is your input matrix that will be processed by column
# y is the scaled output matrix
y <- t(apply(x, 2, function(.col){
.rang <- range(.col)
(.col - .rang[1]) / (.rang[2] - .rang[1]) * 100
}))
On Wed, Jul 16, 2008 at 3:01 PM, rcoder <michael.larsson at gmail.com> wrote:
>
> Here's my code:
>
> nc<-ncol(mat) #setting end point in counter to number of cols in sm
> nr<-nrow(mat)
> mm <- array(NA, dim=c(2, nc)) #to hold min/max ranges
> sc <- array(NA, dim=c(nr, nc)) #to hold percentile scales
> for (n in 1:nc) { #now calculate respective ranges for
> data matrix
> mm[,n]<-range(mat[,n],na.rm=T) #inserts min/max values into sc
> matrix
> for (m in 1:nr) {
> sc[m,n]<-100*(mat[m,n]-mm[1,n])/(mm[2,n]-mm[1,n]) #re-scaling onto
> percentile ranking
> }
> }
> rcoder
>
>
>
> rcoder wrote:
>>
>> I am trying to apply the solution you mentioned to all columns in a
>> matrix, and output the results to another matrix and append the two using
>> the cbind function.
>>
>> I have written something that works, using a nested For loop to go through
>> all the cells in the target matrix, but the trouble is that the process
>> takes a while to run (3-4 mins). The matrix is large, about 2000 by 10000,
>> so this could be a reason for the slow speed. However, I'm convinced there
>> is a faster way of applying this mapping procedure to all columns in a
>> matrix and outoutting into columns in a separate matrix.
>>
>> I would be grateful for any suggestions on this slight modification.
>> Otherwise, I can make do with my version.
>>
>> Thanks,
>>
>> rcoder
>>
>>
>> rcoder wrote:
>>>
>>> Thank you Ben! This is very clear.
>>> rcoder
>>>
>>>
>>> Ben Tupper wrote:
>>>>
>>>>
>>>> On Jul 15, 2008, at 5:16 PM, rcoder wrote:
>>>>
>>>>>
>>>>> Hi Ben,
>>>>> Yes, this is more or less what I want to do. I want to apply this
>>>>> data in
>>>>> columns in a matrix, and insert the results to additional columns.
>>>>> I am not
>>>>> entirely aware of a good way of doing this.
>>>>>
>>>>> e.g. take data from column B, apply normalisation, and feed output
>>>>> into
>>>>> column C
>>>>>
>>>>
>>>> Oh,
>>>>
>>>> I think you want to use cbind() to add columns to a matrix. Perhaps
>>>> like this which works with the second column...
>>>>
>>>> v <- matrix(data = rnorm(100), nrow = 10, ncol = 10)
>>>> mm <- range(v[,2])
>>>> s <- 10 * (v[,2]-mm[1])/(mm[2]-mm[1])
>>>> v2 <- cbind(v, s)
>>>>
>>>> Cheers,
>>>> Ben
>>>>
>>>>
>>>>
>>>>> Thanks,
>>>>>
>>>>> rcoder
>>>>>
>>>>>
>>>>>
>>>>> Ben Tupper wrote:
>>>>>>
>>>>>>
>>>>>> On Jul 15, 2008, at 8:16 AM, rcoder wrote:
>>>>>>
>>>>>>>
>>>>>>> Hi everyone,
>>>>>>>
>>>>>>> I want to score a set of data (-ve to +ve) using a 0-10 scale. I
>>>>>>> have the
>>>>>>> data in an R matrix, so I need to add another column, containing
>>>>>>> the scores
>>>>>>> and resave.
>>>>>>>
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I am a little fuzzy on what you are asking, but my guess is that you
>>>>>> want to normalize the data into the 0-1 range then multiply by 10.
>>>>>>
>>>>>> values <- rnorm(10) #some numbers
>>>>>> mm <- range(values) #the minmax range
>>>>>> scaled <- (values-mm[1])/(mm[2]-mm[1]) #normalize into 0-1
>>>>>> scaled10 <- 10 * scaled #scale 0-10
>>>>>>
>>>>>> Is that what you seek?
>>>>>> Ben
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help at r-project.org mailing list
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>> PLEASE do read the posting guide
>>>>>> http://www.R-project.org/posting-guide.html
>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>
>>>>>>
>>>>>
>>>>> --
>>>>> View this message in context: http://www.nabble.com/Mapping-data-
>>>>> onto-score-tp18463695p18475083.html
>>>>> Sent from the R help mailing list archive at Nabble.com.
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-
>>>>> guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>> Ben Tupper
>>>> PemaquidRiver at tidewater.net
>>>>
>>>> I GoodSearch for Ashwood Waldorf School.
>>>>
>>>> Raise money for your favorite charity or school just by searching the
>>>> Internet with GoodSearch - www.goodsearch.com - powered by Yahoo!
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>>>
>>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Mapping-data-onto-score-tp18463695p18494357.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list