Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-08 Thread Dimitri Liakhovitski
Sorry, I didn't have time to check the speed, indeed.
However - isn't apply the same as a loop, just hidden?
D.

On Fri, Apr 6, 2012 at 6:59 PM, ilai ke...@math.montana.edu wrote:
 On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski
 dimitri.liakhovit...@gmail.com wrote:
  This works great:

 Really ? surprising given it is the EXACT same for-loop as in your
 original problem with counter i replaced by k and reorder to
 matrix[!100]- 0 instead of matrix(0)[i]- 100
 You didn't even attempt to implement Carl's suggestion to use apply
 family for looping (which I still think is completely unnecessary).

 The only logical conclusion is N=nrow(input) was not large enough to
 pose a problem in the first place. In the future please use some brain
 power before waisting ours.

 Cheers


 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 result-input
 N-nrow(input)
 for (k in 1:N){
  foo - which (input == k,arr.ind=T)
  result[k,foo[2]] -100
 }
 result[result !=100]-0

 Dimitri


 On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft c...@witthoft.com wrote:
 I think the OP wants to fill values in an arbitrarily large matrix. Now,
 first of all, I'd like to know what his real problem is, since this seems
 like a very tedious and unproductive matrix to produce.  But in the
 meantime,  since he also left out important information, let's assume the
 input matrix is N rows by M columns, and that he wants therefore to end up
 with N instances of 100, not counting the original value of 100 that is
 one of his ranking values (a bad BAD move IMHO).

 Then either loop or lapply over an equation like (I've expanded things more
 than necessary for clarity
 result-inmatrix
 for (k in 1:N){
 foo - which (inmatrix == k,arr.ind=T)
 result[k,foo[2]] -100

 }



 I maybe missing something but this seems like an indexing problem
 which doesn't require a loop at all. Something like this maybe?

 (input-matrix(c(5,1,3,7,2,6,4,8),nc=2))
 output - matrix(0,max(input),2)
 output[input[,1],1] - 100
 output[input[,2],2] - 100
 output

 Cheers


 On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski
 dimitri.liakhovitski at gmail.com wrote:
 Hello, everybody!

 I have a matrix input (see example below) - with all unique entries
 that are actually unique ranks (i.e., start with 1, no ties).
 I want to assign a value of 100 to the first row of the column that
 contains the minimum (i.e., value of 1).
 Then, I want to assign a value of 100 to the second row of the column
 that contains the value of 2, etc.
 The results I am looking for are in desired.results.
 My code (below) does what I need. But it's using a loop through all
 the rows of my matrix and searches for a matrix element every time.
 My actual matrix is very large. Is there a way to do it more efficiently?
 Thank you very much for the tips!
 Dimitri

 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 (input)
 desired.result-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100)))
 (desired.result)
 result-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0)))
 for(i in 1:nrow(input)){ # i-1
  mymin-i
  mycoords-which(input==mymin,arr.ind=TRUE)
  result[i,mycoords[2]]-100
  input[mycoords]-max(input)
 }
 (result)

 --

 Sent from my Cray XK6
 Quidvis recte factum, quamvis humile, praeclarum.


 __
 R-help@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.



 --
 Dimitri Liakhovitski
 marketfusionanalytics.com

 __
 R-help@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.



-- 
Dimitri Liakhovitski
marketfusionanalytics.com

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-08 Thread Dimitri Liakhovitski
Thank you very much, Rui.
This definitely produces the result needed.
Again, I have not checked the speed yet.

input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
f - function(x){
   nr - nrow(x)
   result - matrix(0, nrow=nr, ncol=ncol(x))
   colnames(result) - colnames(x)

   inp.ord - order(x)[1:nr] - 1 # Keep only one per row, must be zero based
   inx - cbind(1:nr, inp.ord %/% nr + 1) # Index matrix
   result[inx] - 100
   result
}
f(input)

Dimitri

On Fri, Apr 6, 2012 at 7:02 PM, Rui Barradas rui1...@sapo.pt wrote:
 Hello,

 Oops!

 What happened to the function 'f'?
 Forgot to copy and pasted only the rest, now complete.


 f - function(x){
        nr - nrow(x)
        result - matrix(0, nrow=nr, ncol=ncol(x))
        colnames(result) - colnames(x)

        inp.ord - order(x)[1:nr] - 1 # Keep only one per row, must be zero 
 based
        inx - cbind(1:nr, inp.ord %/% nr + 1) # Index matrix
        result[inx] - 100
        result
 }

 # Original example
 input - as.matrix(data.frame(a=c(5,1,3,7), b=c(2,6,4,8)))
 (input)
 desired.result - as.matrix(data.frame(a=c(100,0,100,0), b=c(0,100,0,100)))
 (desired.result)
 all.equal(f(input), desired.result)

 # Two other examples
 set.seed(123)
 (x - matrix(sample(10, 10), ncol=2))
 f(x)

 (y - matrix(sample(40, 40), ncol=5))
 f(y)

 Note that there's no loops (or apply, which is also a loop.)

 Rui Barradas


 --
 View this message in context: 
 http://r.789695.n4.nabble.com/filling-the-matrix-row-by-row-in-the-order-from-lower-to-larger-elements-tp4538171p4538486.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.



-- 
Dimitri Liakhovitski
marketfusionanalytics.com

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-08 Thread Bert Gunter
Preoccupation with speed of execution is typically (certainly not
always) misplaced. Provided you have used sensible basic
vectorization, loops in whatever form work adequately. First get
working code. Then, if necessary, parallelization, byte compilation,
or complex vectorization strategies can be employed. And, of course,
there's always C code for those who know it.

-- Bert

On Sun, Apr 8, 2012 at 7:31 AM, Dimitri Liakhovitski
dimitri.liakhovit...@gmail.com wrote:
 Thank you very much, Rui.
 This definitely produces the result needed.
 Again, I have not checked the speed yet.

 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 f - function(x){
       nr - nrow(x)
       result - matrix(0, nrow=nr, ncol=ncol(x))
       colnames(result) - colnames(x)

       inp.ord - order(x)[1:nr] - 1 # Keep only one per row, must be zero 
 based
       inx - cbind(1:nr, inp.ord %/% nr + 1) # Index matrix
       result[inx] - 100
       result
 }
 f(input)

 Dimitri

 On Fri, Apr 6, 2012 at 7:02 PM, Rui Barradas rui1...@sapo.pt wrote:
 Hello,

 Oops!

 What happened to the function 'f'?
 Forgot to copy and pasted only the rest, now complete.


 f - function(x){
        nr - nrow(x)
        result - matrix(0, nrow=nr, ncol=ncol(x))
        colnames(result) - colnames(x)

        inp.ord - order(x)[1:nr] - 1 # Keep only one per row, must be zero 
 based
        inx - cbind(1:nr, inp.ord %/% nr + 1) # Index matrix
        result[inx] - 100
        result
 }

 # Original example
 input - as.matrix(data.frame(a=c(5,1,3,7), b=c(2,6,4,8)))
 (input)
 desired.result - as.matrix(data.frame(a=c(100,0,100,0), b=c(0,100,0,100)))
 (desired.result)
 all.equal(f(input), desired.result)

 # Two other examples
 set.seed(123)
 (x - matrix(sample(10, 10), ncol=2))
 f(x)

 (y - matrix(sample(40, 40), ncol=5))
 f(y)

 Note that there's no loops (or apply, which is also a loop.)

 Rui Barradas


 --
 View this message in context: 
 http://r.789695.n4.nabble.com/filling-the-matrix-row-by-row-in-the-order-from-lower-to-larger-elements-tp4538171p4538486.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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.



 --
 Dimitri Liakhovitski
 marketfusionanalytics.com

 __
 R-help@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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-08 Thread ilai
On Sun, Apr 8, 2012 at 8:26 AM, Dimitri Liakhovitski
dimitri.liakhovit...@gmail.com wrote:
 Sorry, I didn't have time to check the speed, indeed.
 However - isn't apply the same as a loop, just hidden?
 D.


Yes ?apply is a loop but not the same as ?for, see Intro to R. As
Bert Gunter pointed out the issue here was not speed but that your
problem could have been vectorized to avoid loops all together (which
would have the added benefit of speed). This was given to you in my
first response which assumed a small number of columns in the matrix,
but Rui gave an elegant expansion to use on any ncol(matrix). Using
apply was suggested at some point by others, my comment was simply
that you failed to meet even that adjustment.
Cheers



 On Fri, Apr 6, 2012 at 6:59 PM, ilai ke...@math.montana.edu wrote:
 On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski
 dimitri.liakhovit...@gmail.com wrote:
  This works great:

 Really ? surprising given it is the EXACT same for-loop as in your
 original problem with counter i replaced by k and reorder to
 matrix[!100]- 0 instead of matrix(0)[i]- 100
 You didn't even attempt to implement Carl's suggestion to use apply
 family for looping (which I still think is completely unnecessary).

 The only logical conclusion is N=nrow(input) was not large enough to
 pose a problem in the first place. In the future please use some brain
 power before waisting ours.

 Cheers


 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 result-input
 N-nrow(input)
 for (k in 1:N){
  foo - which (input == k,arr.ind=T)
  result[k,foo[2]] -100
 }
 result[result !=100]-0

 Dimitri


 On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft c...@witthoft.com wrote:
 I think the OP wants to fill values in an arbitrarily large matrix. Now,
 first of all, I'd like to know what his real problem is, since this seems
 like a very tedious and unproductive matrix to produce.  But in the
 meantime,  since he also left out important information, let's assume the
 input matrix is N rows by M columns, and that he wants therefore to end up
 with N instances of 100, not counting the original value of 100 that is
 one of his ranking values (a bad BAD move IMHO).

 Then either loop or lapply over an equation like (I've expanded things more
 than necessary for clarity
 result-inmatrix
 for (k in 1:N){
 foo - which (inmatrix == k,arr.ind=T)
 result[k,foo[2]] -100

 }



 I maybe missing something but this seems like an indexing problem
 which doesn't require a loop at all. Something like this maybe?

 (input-matrix(c(5,1,3,7,2,6,4,8),nc=2))
 output - matrix(0,max(input),2)
 output[input[,1],1] - 100
 output[input[,2],2] - 100
 output

 Cheers


 On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski
 dimitri.liakhovitski at gmail.com wrote:
 Hello, everybody!

 I have a matrix input (see example below) - with all unique entries
 that are actually unique ranks (i.e., start with 1, no ties).
 I want to assign a value of 100 to the first row of the column that
 contains the minimum (i.e., value of 1).
 Then, I want to assign a value of 100 to the second row of the column
 that contains the value of 2, etc.
 The results I am looking for are in desired.results.
 My code (below) does what I need. But it's using a loop through all
 the rows of my matrix and searches for a matrix element every time.
 My actual matrix is very large. Is there a way to do it more efficiently?
 Thank you very much for the tips!
 Dimitri

 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 (input)
 desired.result-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100)))
 (desired.result)
 result-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0)))
 for(i in 1:nrow(input)){ # i-1
  mymin-i
  mycoords-which(input==mymin,arr.ind=TRUE)
  result[i,mycoords[2]]-100
  input[mycoords]-max(input)
 }
 (result)

 --

 Sent from my Cray XK6
 Quidvis recte factum, quamvis humile, praeclarum.


 __
 R-help@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.



 --
 Dimitri Liakhovitski
 marketfusionanalytics.com

 __
 R-help@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.



 --
 Dimitri Liakhovitski
 marketfusionanalytics.com

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-08 Thread Dimitri Liakhovitski
Agreed, Rui provided a very elegant vectorized solution - and I am
very thankful to him. Unfortunately (for myself), I am not as
proficient in vectorization - otherwise, I would not have asked the
question.
Why I did not follow on the original hint to use apply? For this reason (quote):
 However - isn't apply the same as a loop, just hidden?
with slight caveats, yes.
Bert

Dimitri

On Sun, Apr 8, 2012 at 3:16 PM, ilai ke...@math.montana.edu wrote:
 On Sun, Apr 8, 2012 at 8:26 AM, Dimitri Liakhovitski
 dimitri.liakhovit...@gmail.com wrote:
 Sorry, I didn't have time to check the speed, indeed.
 However - isn't apply the same as a loop, just hidden?
 D.


 Yes ?apply is a loop but not the same as ?for, see Intro to R. As
 Bert Gunter pointed out the issue here was not speed but that your
 problem could have been vectorized to avoid loops all together (which
 would have the added benefit of speed). This was given to you in my
 first response which assumed a small number of columns in the matrix,
 but Rui gave an elegant expansion to use on any ncol(matrix). Using
 apply was suggested at some point by others, my comment was simply
 that you failed to meet even that adjustment.
 Cheers



 On Fri, Apr 6, 2012 at 6:59 PM, ilai ke...@math.montana.edu wrote:
 On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski
 dimitri.liakhovit...@gmail.com wrote:
  This works great:

 Really ? surprising given it is the EXACT same for-loop as in your
 original problem with counter i replaced by k and reorder to
 matrix[!100]- 0 instead of matrix(0)[i]- 100
 You didn't even attempt to implement Carl's suggestion to use apply
 family for looping (which I still think is completely unnecessary).

 The only logical conclusion is N=nrow(input) was not large enough to
 pose a problem in the first place. In the future please use some brain
 power before waisting ours.

 Cheers


 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 result-input
 N-nrow(input)
 for (k in 1:N){
  foo - which (input == k,arr.ind=T)
  result[k,foo[2]] -100
 }
 result[result !=100]-0

 Dimitri


 On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft c...@witthoft.com wrote:
 I think the OP wants to fill values in an arbitrarily large matrix. Now,
 first of all, I'd like to know what his real problem is, since this seems
 like a very tedious and unproductive matrix to produce.  But in the
 meantime,  since he also left out important information, let's assume the
 input matrix is N rows by M columns, and that he wants therefore to end up
 with N instances of 100, not counting the original value of 100 that is
 one of his ranking values (a bad BAD move IMHO).

 Then either loop or lapply over an equation like (I've expanded things 
 more
 than necessary for clarity
 result-inmatrix
 for (k in 1:N){
 foo - which (inmatrix == k,arr.ind=T)
 result[k,foo[2]] -100

 }



 I maybe missing something but this seems like an indexing problem
 which doesn't require a loop at all. Something like this maybe?

 (input-matrix(c(5,1,3,7,2,6,4,8),nc=2))
 output - matrix(0,max(input),2)
 output[input[,1],1] - 100
 output[input[,2],2] - 100
 output

 Cheers


 On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski
 dimitri.liakhovitski at gmail.com wrote:
 Hello, everybody!

 I have a matrix input (see example below) - with all unique entries
 that are actually unique ranks (i.e., start with 1, no ties).
 I want to assign a value of 100 to the first row of the column that
 contains the minimum (i.e., value of 1).
 Then, I want to assign a value of 100 to the second row of the column
 that contains the value of 2, etc.
 The results I am looking for are in desired.results.
 My code (below) does what I need. But it's using a loop through all
 the rows of my matrix and searches for a matrix element every time.
 My actual matrix is very large. Is there a way to do it more efficiently?
 Thank you very much for the tips!
 Dimitri

 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 (input)
 desired.result-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100)))
 (desired.result)
 result-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0)))
 for(i in 1:nrow(input)){ # i-1
  mymin-i
  mycoords-which(input==mymin,arr.ind=TRUE)
  result[i,mycoords[2]]-100
  input[mycoords]-max(input)
 }
 (result)

 --

 Sent from my Cray XK6
 Quidvis recte factum, quamvis humile, praeclarum.


 __
 R-help@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.



 --
 Dimitri Liakhovitski
 marketfusionanalytics.com

 __
 R-help@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.



 --
 Dimitri 

Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-07 Thread Rui Barradas
Hello,

Oops!

What happened to the function 'f'?
Forgot to copy and pasted only the rest, now complete.


f - function(x){
nr - nrow(x)
result - matrix(0, nrow=nr, ncol=ncol(x))
colnames(result) - colnames(x)

inp.ord - order(x)[1:nr] - 1 # Keep only one per row, must be zero 
based
inx - cbind(1:nr, inp.ord %/% nr + 1) # Index matrix
result[inx] - 100
result
}

# Original example
input - as.matrix(data.frame(a=c(5,1,3,7), b=c(2,6,4,8)))
(input)
desired.result - as.matrix(data.frame(a=c(100,0,100,0), b=c(0,100,0,100)))
(desired.result)
all.equal(f(input), desired.result)

# Two other examples
set.seed(123)
(x - matrix(sample(10, 10), ncol=2))
f(x)

(y - matrix(sample(40, 40), ncol=5))
f(y)

Note that there's no loops (or apply, which is also a loop.)

Rui Barradas


--
View this message in context: 
http://r.789695.n4.nabble.com/filling-the-matrix-row-by-row-in-the-order-from-lower-to-larger-elements-tp4538171p4538486.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-07 Thread Rui Barradas
Hello,


 I maybe missing something but this seems like an indexing problem
 which doesn't require a loop at all.
 

Yes, but with 'order'.


# Original example
input - as.matrix(data.frame(a=c(5,1,3,7), b=c(2,6,4,8)))
(input)
desired.result - as.matrix(data.frame(a=c(100,0,100,0), b=c(0,100,0,100)))
(desired.result)
all.equal(f(input), desired.result)

# Two other examples
set.seed(123)
(x - matrix(sample(10, 10), ncol=2))
f(x)

(y - matrix(sample(40, 40), ncol=5))
f(y)


Hope this helps,

Rui Barradas


--
View this message in context: 
http://r.789695.n4.nabble.com/filling-the-matrix-row-by-row-in-the-order-from-lower-to-larger-elements-tp4538171p4538334.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-06 Thread ilai
I maybe missing something but this seems like an indexing problem
which doesn't require a loop at all. Something like this maybe?

(input-matrix(c(5,1,3,7,2,6,4,8),nc=2))
output - matrix(0,max(input),2)
output[input[,1],1] - 100
output[input[,2],2] - 100
output

Cheers


On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski
dimitri.liakhovit...@gmail.com wrote:
 Hello, everybody!

 I have a matrix input (see example below) - with all unique entries
 that are actually unique ranks (i.e., start with 1, no ties).
 I want to assign a value of 100 to the first row of the column that
 contains the minimum (i.e., value of 1).
 Then, I want to assign a value of 100 to the second row of the column
 that contains the value of 2, etc.
 The results I am looking for are in desired.results.
 My code (below) does what I need. But it's using a loop through all
 the rows of my matrix and searches for a matrix element every time.
 My actual matrix is very large. Is there a way to do it more efficiently?
 Thank you very much for the tips!
 Dimitri

 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 (input)
 desired.result-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100)))
 (desired.result)
 result-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0)))
 for(i in 1:nrow(input)){ # i-1
  mymin-i
  mycoords-which(input==mymin,arr.ind=TRUE)
  result[i,mycoords[2]]-100
  input[mycoords]-max(input)
 }
 (result)

 --
 Dimitri Liakhovitski

 __
 R-help@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.

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-06 Thread Carl Witthoft
I think the OP wants to fill values in an arbitrarily large matrix. 
Now, first of all, I'd like to know what his real problem is, since this 
seems like a very tedious and unproductive matrix to produce.  But in 
the meantime,  since he also left out important information, let's 
assume the input matrix is N rows by M columns, and that he wants 
therefore to end up with N instances of 100, not counting the original 
value of 100 that is one of his ranking values (a bad BAD move IMHO).


Then either loop or lapply over an equation like (I've expanded things 
more than necessary for clarity

result-inmatrix
for (k in 1:N){
foo - which (inmatrix == k,arr.ind=T)
result[k,foo[2]] -100
}



I maybe missing something but this seems like an indexing problem
which doesn't require a loop at all. Something like this maybe?

(input-matrix(c(5,1,3,7,2,6,4,8),nc=2))
output - matrix(0,max(input),2)
output[input[,1],1] - 100
output[input[,2],2] - 100
output

Cheers


On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski
dimitri.liakhovitski at gmail.com wrote:
 Hello, everybody!

 I have a matrix input (see example below) - with all unique entries
 that are actually unique ranks (i.e., start with 1, no ties).
 I want to assign a value of 100 to the first row of the column that
 contains the minimum (i.e., value of 1).
 Then, I want to assign a value of 100 to the second row of the column
 that contains the value of 2, etc.
 The results I am looking for are in desired.results.
 My code (below) does what I need. But it's using a loop through all
 the rows of my matrix and searches for a matrix element every time.
 My actual matrix is very large. Is there a way to do it more efficiently?
 Thank you very much for the tips!
 Dimitri

 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 (input)
 desired.result-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100)))
 (desired.result)
 result-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0)))
 for(i in 1:nrow(input)){ # i-1
  mymin-i
  mycoords-which(input==mymin,arr.ind=TRUE)
  result[i,mycoords[2]]-100
  input[mycoords]-max(input)
 }
 (result)

--

Sent from my Cray XK6
Quidvis recte factum, quamvis humile, praeclarum.

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-06 Thread Carl Witthoft

Ok, how's this:


Rgames foo
 [,1] [,2] [,3] [,4]
[1,]361   16
[2,]   10   14   125
[3,]   117   159
[4,]84   132

Rgames sapply(1:4,FUN=function(k){ 
foo[k,which(foo==k,arr.ind=T)[2]]-100;return(foo)})-bar

Rgames bar
  [,1] [,2] [,3] [,4]
 [1,]3333
 [2,]   10   10   10   10
 [3,]   11   11  100   11
 [4,]8888
 [5,]6666
 [6,]   14   14   14   14
 [7,]7777
 [8,]444  100
 [9,]  100111
[10,]   12   12   12   12
[11,]   15   15   15   15
[12,]   13   13   13   13
[13,]   16   16   16   16
[14,]5  10055
[15,]9999
[16,]2222


Rgames rab-matrix(apply(bar,1,max),4,4)
Rgames rab
 [,1] [,2] [,3] [,4]
[1,]36  100   16
[2,]   10   14   12  100
[3,]  1007   159
[4,]8  100   132


--

Sent from my Cray XK6
Quidvis recte factum, quamvis humile, praeclarum.

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-06 Thread Dimitri Liakhovitski
Yes, that's correct - my matrix has N rows.
Thank you very much, Carl. This works great:

input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
result-input
N-nrow(input)
for (k in 1:N){
  foo - which (input == k,arr.ind=T)
  result[k,foo[2]] -100
}
result[result !=100]-0

Dimitri


On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft c...@witthoft.com wrote:
 I think the OP wants to fill values in an arbitrarily large matrix. Now,
 first of all, I'd like to know what his real problem is, since this seems
 like a very tedious and unproductive matrix to produce.  But in the
 meantime,  since he also left out important information, let's assume the
 input matrix is N rows by M columns, and that he wants therefore to end up
 with N instances of 100, not counting the original value of 100 that is
 one of his ranking values (a bad BAD move IMHO).

 Then either loop or lapply over an equation like (I've expanded things more
 than necessary for clarity
 result-inmatrix
 for (k in 1:N){
 foo - which (inmatrix == k,arr.ind=T)
 result[k,foo[2]] -100

 }



 I maybe missing something but this seems like an indexing problem
 which doesn't require a loop at all. Something like this maybe?

 (input-matrix(c(5,1,3,7,2,6,4,8),nc=2))
 output - matrix(0,max(input),2)
 output[input[,1],1] - 100
 output[input[,2],2] - 100
 output

 Cheers


 On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski
 dimitri.liakhovitski at gmail.com wrote:
 Hello, everybody!

 I have a matrix input (see example below) - with all unique entries
 that are actually unique ranks (i.e., start with 1, no ties).
 I want to assign a value of 100 to the first row of the column that
 contains the minimum (i.e., value of 1).
 Then, I want to assign a value of 100 to the second row of the column
 that contains the value of 2, etc.
 The results I am looking for are in desired.results.
 My code (below) does what I need. But it's using a loop through all
 the rows of my matrix and searches for a matrix element every time.
 My actual matrix is very large. Is there a way to do it more efficiently?
 Thank you very much for the tips!
 Dimitri

 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 (input)
 desired.result-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100)))
 (desired.result)
 result-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0)))
 for(i in 1:nrow(input)){ # i-1
  mymin-i
  mycoords-which(input==mymin,arr.ind=TRUE)
  result[i,mycoords[2]]-100
  input[mycoords]-max(input)
 }
 (result)

 --

 Sent from my Cray XK6
 Quidvis recte factum, quamvis humile, praeclarum.


 __
 R-help@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.



-- 
Dimitri Liakhovitski
marketfusionanalytics.com

__
R-help@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.


Re: [R] filling the matrix row by row in the order from lower to larger elements

2012-04-06 Thread ilai
On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski
dimitri.liakhovit...@gmail.com wrote:
 This works great:

Really ? surprising given it is the EXACT same for-loop as in your
original problem with counter i replaced by k and reorder to
matrix[!100]- 0 instead of matrix(0)[i]- 100
You didn't even attempt to implement Carl's suggestion to use apply
family for looping (which I still think is completely unnecessary).

The only logical conclusion is N=nrow(input) was not large enough to
pose a problem in the first place. In the future please use some brain
power before waisting ours.

Cheers


 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 result-input
 N-nrow(input)
 for (k in 1:N){
  foo - which (input == k,arr.ind=T)
  result[k,foo[2]] -100
 }
 result[result !=100]-0

 Dimitri


 On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft c...@witthoft.com wrote:
 I think the OP wants to fill values in an arbitrarily large matrix. Now,
 first of all, I'd like to know what his real problem is, since this seems
 like a very tedious and unproductive matrix to produce.  But in the
 meantime,  since he also left out important information, let's assume the
 input matrix is N rows by M columns, and that he wants therefore to end up
 with N instances of 100, not counting the original value of 100 that is
 one of his ranking values (a bad BAD move IMHO).

 Then either loop or lapply over an equation like (I've expanded things more
 than necessary for clarity
 result-inmatrix
 for (k in 1:N){
 foo - which (inmatrix == k,arr.ind=T)
 result[k,foo[2]] -100

 }



 I maybe missing something but this seems like an indexing problem
 which doesn't require a loop at all. Something like this maybe?

 (input-matrix(c(5,1,3,7,2,6,4,8),nc=2))
 output - matrix(0,max(input),2)
 output[input[,1],1] - 100
 output[input[,2],2] - 100
 output

 Cheers


 On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski
 dimitri.liakhovitski at gmail.com wrote:
 Hello, everybody!

 I have a matrix input (see example below) - with all unique entries
 that are actually unique ranks (i.e., start with 1, no ties).
 I want to assign a value of 100 to the first row of the column that
 contains the minimum (i.e., value of 1).
 Then, I want to assign a value of 100 to the second row of the column
 that contains the value of 2, etc.
 The results I am looking for are in desired.results.
 My code (below) does what I need. But it's using a loop through all
 the rows of my matrix and searches for a matrix element every time.
 My actual matrix is very large. Is there a way to do it more efficiently?
 Thank you very much for the tips!
 Dimitri

 input-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
 (input)
 desired.result-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100)))
 (desired.result)
 result-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0)))
 for(i in 1:nrow(input)){ # i-1
  mymin-i
  mycoords-which(input==mymin,arr.ind=TRUE)
  result[i,mycoords[2]]-100
  input[mycoords]-max(input)
 }
 (result)

 --

 Sent from my Cray XK6
 Quidvis recte factum, quamvis humile, praeclarum.


 __
 R-help@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.



 --
 Dimitri Liakhovitski
 marketfusionanalytics.com

 __
 R-help@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.

__
R-help@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.