[R] Need some help in R : value more than equals to a row.

Jakson Alves de Aquino jaksonaquino at gmail.com
Sun Jun 7 19:46:10 CEST 2009


suparna mitra wrote:
> Hallo,
>  I was trying some code, but couldn't make one step of the code properly.
> Can anybody  please  help me?
> 
> I have one matrix like this
>> values
>           [,1]       [,2]       [,3]      [,4]      [,5]
> [1,] 0.7777778 0.36111111 0.22222222 0.1388889 0.0000000
> [2,] 1.0000000 0.00000000 0.53846154 0.0000000 0.5384615
> [3,] 0.5200000 0.48000000 0.64000000 0.0000000 0.8800000
> [4,] 0.8928571 1.00000000 0.00000000 0.8928571 0.1071429
> 
> 
> And I want to get some matrix like:
>> values.new
>       [,1]  [,2]  [,3]  [,4]  [,5]
> [1,] 0.2  0.4  0.6  0.8  1.0
> [2,] 0.2  1.0  0.6  1.0  0.6
> [3,] 0.6  0.8  0.4  1.0  0.2
> [4,] 0.6  0.2  1.0  0.6  0.8
> 
> 
> This table should be computed by taking proportion of values in the row that
> are larger or equals to the value being considered with the total no of
> objects in the row.

I think the code below do what you want, but I would like to know how to
do it with one of the *apply functions:

m <- read.table(stdin(), header=F)
0.7777778 0.36111111 0.22222222 0.1388889 0.0000000
1.0000000 0.00000000 0.53846154 0.0000000 0.5384615
0.5200000 0.48000000 0.64000000 0.0000000 0.8800000
0.8928571 1.00000000 0.00000000 0.8928571 0.1071429

m <- as.matrix(m)
bdim <- dim(m)
m2 <- m
for(i in 1:bdim[1])
  for(j in 1:bdim[2])
    m2[i,j] <- sum(m[i,j] <= m[i,]) / bdim[2]
m2




More information about the R-help mailing list