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 -
On Sun, Apr 8, 2012 at 8:26 AM, Dimitri Liakhovitski
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 sp
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
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)
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 wrote:
> On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski
> wrote:
> This works great:
>
> Really ? surprising given it is the EXACT same for-
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,
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 ro
On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski
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 Ca
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,
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,]
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
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
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 t
13 matches
Mail list logo