Re: [R] Help with Book example of Matrix application

2012-04-09 Thread David Winsemius


On Apr 9, 2012, at 12:32 PM, James Lenihan wrote:

I found this example in an Introductory R book in the chapter on  
Matrices and

Arrays

The array is

m

[,1] [,2] [,3] [,4] [,5]
[1,]0   12   138   20
[2,]   120   15   28   88
[3,]   13   15069
[4,]8   2860   33
[5,]   20   889   330

The code is

#returns the minimum value of d[i,j], i !=j and the row attaining
#the minimum, for square symmetric d; no special policy on ties


Would you consider a more compact operation? That code seems very unR- 
ish.


 min( m[row(m) != col(m)] )
#[1] 6
 which(m ==  min( m[row(m) != col(m)] ), arr.ind=TRUE)
#
 row col
[1,]   4   3
[2,]   3   4
---
 c(min= min( m[row(m) != col(m)] ),
   which(m ==  min( m[row(m) != col(m)] ), arr.ind=TRUE)[1,])

min row col
  6   4   3


I suppose the question could be how to debug that code, in which case  
you should be manually "stepping through" it to see what the values  
are during the implicit loop.



mind  <- function(d) {
n  <- nrow(d)
# add a column to identify row number for apply()
dd  <- cbind(d,1:n)
wmins <- apply(dd[-n,],1,imin)
# wins will be 2 X n, 1st row being indices and 2nd being values
i <- which.min(wmins[2,])
j <- wmins[1,i]
return(c(d[i,j],i,j))
}

#this finds the location, value of the minimum in a row x
imin  <- function(x) {
  lx  <- length(x)
   i  <- x[lx] # original row number
   j <-which.min(x[(x+1):(lx-1)])
   k <- i+j
   return(c(k,x[k]))
}
The result s/b
[1] 6 3 4

I am getting:

Warning messages:
1: In (x + 1):(lx - 1) :
 numerical expression has 6 elements: only the first used
2: In (x + 1):(lx - 1) :
 numerical expression has 6 elements: only the first used
3: In (x + 1):(lx - 1) :
 numerical expression has 6 elements: only the first used
4: In (x + 1):(lx - 1) :
 numerical expression has 6 elements: only the first used

I have check my typing a number of times.

Does anyone see an error?

Thanks,

Jim L.
[[alternative HTML version deleted]]

__
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.


David Winsemius, MD
West Hartford, CT

__
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] Help with Book example of Matrix application

2012-04-09 Thread Duncan Murdoch

On 09/04/2012 12:32 PM, James Lenihan wrote:

I found this example in an Introductory R book in the chapter on Matrices and
Arrays


You should probably check with the author of the book (there might be an 
errata page posted somewhere), but it looks like a typo, in your code or 
the original:


The array is
>  m
  [,1] [,2] [,3] [,4] [,5]
[1,]0   12   138   20
[2,]   120   15   28   88
[3,]   13   15069
[4,]8   2860   33
[5,]   20   889   330

The code is

#returns the minimum value of d[i,j], i !=j and the row attaining
#the minimum, for square symmetric d; no special policy on ties
mind<- function(d) {
  n<- nrow(d)
  # add a column to identify row number for apply()
  dd<- cbind(d,1:n)
  wmins<- apply(dd[-n,],1,imin)
  # wins will be 2 X n, 1st row being indices and 2nd being values
  i<- which.min(wmins[2,])
  j<- wmins[1,i]
  return(c(d[i,j],i,j))
}

#this finds the location, value of the minimum in a row x
imin<- function(x) {
lx<- length(x)
 i<- x[lx] # original row number
 j<-which.min(x[(x+1):(lx-1)])


That line doesn't make sense.  Putting i in place of the innermost x 
would make more sense, but wouldn't work in all cases:  if i == lx, 
you'll get a garbage answer.


Duncan Murdoch

 k<- i+j
 return(c(k,x[k]))
}
The result s/b
[1] 6 3 4

I am getting:

Warning messages:
1: In (x + 1):(lx - 1) :
   numerical expression has 6 elements: only the first used
2: In (x + 1):(lx - 1) :
   numerical expression has 6 elements: only the first used
3: In (x + 1):(lx - 1) :
   numerical expression has 6 elements: only the first used
4: In (x + 1):(lx - 1) :
   numerical expression has 6 elements: only the first used

I have check my typing a number of times.

Does anyone see an error?

Thanks,

Jim L.
[[alternative HTML version deleted]]

__
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.


[R] Help with Book example of Matrix application

2012-04-09 Thread James Lenihan
I found this example in an Introductory R book in the chapter on Matrices and 
Arrays

The array is 
> m
 [,1] [,2] [,3] [,4] [,5]
[1,]0   12   138   20
[2,]   120   15   28   88
[3,]   13   15069
[4,]8   2860   33
[5,]   20   889   330

The code is  

#returns the minimum value of d[i,j], i !=j and the row attaining 
#the minimum, for square symmetric d; no special policy on ties  
mind  <- function(d) {
 n  <- nrow(d)
 # add a column to identify row number for apply()
 dd  <- cbind(d,1:n)
 wmins <- apply(dd[-n,],1,imin)
 # wins will be 2 X n, 1st row being indices and 2nd being values
 i <- which.min(wmins[2,])
 j <- wmins[1,i]
 return(c(d[i,j],i,j))
}

#this finds the location, value of the minimum in a row x
imin  <- function(x) {
   lx  <- length(x)
i  <- x[lx] # original row number
j <-which.min(x[(x+1):(lx-1)]) 
k <- i+j
return(c(k,x[k]))
}
The result s/b
[1] 6 3 4

I am getting:

Warning messages:
1: In (x + 1):(lx - 1) :
  numerical expression has 6 elements: only the first used
2: In (x + 1):(lx - 1) :
  numerical expression has 6 elements: only the first used
3: In (x + 1):(lx - 1) :
  numerical expression has 6 elements: only the first used
4: In (x + 1):(lx - 1) :
  numerical expression has 6 elements: only the first used

I have check my typing a number of times. 

Does anyone see an error?

Thanks,

Jim L.
[[alternative HTML version deleted]]

__
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.