Re: [R] search algorithm

2006-02-13 Thread James Muller
Hi,

I'm not sure how much weight you're putting on efficiency for your
algorithm, but that sounds unnecessarily complicated. If your matrices
are not too big then maybe something like this would work. Let your
matrix be M and your value be a, then


yoursearch - function(M, a) {
# generate abs dist vector
dist - abs(M[,X]-a)

# which element is min dist
argmin - which.min(dist)

# return corresponding Y value
M[argmin,Y]
}


There are so many ways to do this faster (google for search algorithms
if you feel so inclined), but I think simplicity is good unless you
have enormous matrices or need to do it a very large number of times.

Anyway, hope this helps

James


On Mon, Feb 13, 2006 at 01:30:21PM +0100, [EMAIL PROTECTED] wrote:
 I have a problem of finding a specific value in a column. For example, I
 have a matrix with say 2 columns
 
 Now if I have some value of x=-0.6523. I need to find a value in the X
 column  that is the closest to my value of x then read off the row number
 and then take the corresponding value in column Y. What I am not sure is
 how to do the first search where I would search by decimal places and take
 the smallest absolute distance between the numbers. For example if he finds
 the first value which is correct in this case - and then 0 and then 6 and
 then 5 but now there is no 2 for that specific decimal place so he would
 calculate the distance between the one before and the one after and see
 which one is smaller. For that which is smaller would be the final X value.
 Can someone please give me a hint on how to proceed. Thanks.

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] Error: cannot allocate vector of size... but with a twist

2005-01-29 Thread James Muller
correction: I actually did run things before (as the gc output 
indicates), but deleted them from memory before. Everything else applied 
anyway.

James
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] Error: cannot allocate vector of size... but with a twist

2005-01-27 Thread James Muller
Hi,
I have a memory problem, one which I've seen pop up in the list a few 
times, but which seems to be a little different. It is the Error: cannot 
allocate vector of size x problem. I'm running R2.0 on RH9.

My R program is joining big datasets together, so there are lots of 
duplicate cases of data in memory. This (and other tasks) prompted me 
to... expand... my swap partition to 16Gb. I have 0.5Gb of regular, fast 
DDR. The OS seems to be fine accepting the large amount of memory, and 
I'm not restricting memory use or vector size in any way.

R chews up memory up until the 3.5Gb area, then halts. Here's the last 
bit of output:

 # join the data together
 cdata01.data - 
cbind(c.1,c.2,c.3,c.4,c.5,c.6,c.7,c.8,c.9,c.10,c.11,c.12,c.13,c.14,c.15,c.16,c.17,c.18,c.19,c.20,c.21,c.22,c.23,c.24,c.25,c.26,c.27,c.28,c.29,c.30,c.31,c.32,c.33)
Error: cannot allocate vector of size 145 Kb
Execution halted

145--Kb---?? This has me rather lost. Maybe on overflow of some sort?? 
Maybe on OS problem of some sort? I'm scratching here.

Before you question it, there is a legitimate reason for sticking all 
these components in the one data.frame.

One of the problems here is that tinkering is not really feasible. This 
cbind took 1.5 hrs to finally halt.

Any help greatly appreciated,
James
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] Excel *.xls files, RODBC

2004-12-06 Thread James Muller
There is also a perl module that converts excel files to .csv on CPAN. 
It works fine for everything I've ever used it for, which is really 
simple stuff, i.e. no cells defined by functions.

steps involved:
1. go to www.cpan.org and find the package, download it
2. ensure you have the necessary setup to do things with perl, otherwise 
set them up
3. install the package
4. use it as the perl script instructs (i.e. scriptname --help or 
something similar)

Cheers
James
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] convert Map shapes to logical matrices - for set operations

2004-12-06 Thread James Muller
Hi, I have a little problem.
I'm trying to do the following:
 Convert  _projected_ shapes from a Map object into logical matrices. 
That is, rasterize a shape into a logical in-the-shape and 
out-of-the-shape matrix.

What I'm trying to do is get an 'equal-area' estimate of the area of 
intersection between two overlapping shapes, without any concern about 
there being holes in the shapes. I know owin can deal with most of this, 
but for sure it doesn't support geographic projections, which will allow 
the 'equal-area' estimate to be equal.

An idea I had was to pipe the output of the maptools Map plotting 
function map into the matrix. But I really have no idea how to do that 
at this point in my R education.

Any suggestions are extremely welcome.
Cheers,
James
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] unavoidable loop? a better way??

2004-11-12 Thread James Muller
Hi all, I have the following problem, best expressed by my present  
solution:

# p is a vector
myfunc - function (p) {
  x[1] - p[1]
  for (i in c(2:length(p))) {
x[i] - 0.8*p[i] + 0.2*p[i-1]
  }
  return (x)
}
That is, I'm calculating a time-weighted average. Unfortunately the scale  
of the problem is big. length(p) in this case is such that each call takes  
about 6 seconds, and I have to call it about 2000 times (~3 hours). And,  
I'd like to do this each day. Thus, a more efficient method is desirable.

Of course, this could be done faster by writing it in c, but I want to  
avoid doing that if there already exists something internal to do the  
operation quickly (because I've never programmed c for use in R).

Can anybody offer a solution?
I apologise if this is a naive question.
James
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] unavoidable loop? a better way??

2004-11-12 Thread James Muller
I am very sorry. I've made a typo. The function should be:
# p is a vector
myfunc - function (p) {
  x[1] - p[1]
  for (i in c(2:length(p))) {
x[i] - 0.8*p[i] + 0.2*x[i-1]
  }
  return (x)
}
James
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] unavoidable loop? a better way??

2004-11-12 Thread James Muller
Take 3:
# p is a vector
myfunc - function (p) {
   x - rep(0,length(p))
   x[1] - p[1]
   for (i in c(2:length(p))) {
 x[i] - 0.8*p[i] + 0.2*x[i-1]   # note the x in the last term
   }
   return (x)
}
James


On Sat, 13 Nov 2004 01:12:50 -0600, Deepayan Sarkar  
[EMAIL PROTECTED] wrote:

On Saturday 13 November 2004 00:51, James Muller wrote:
Hi all, I have the following problem, best expressed by my present
solution:
# p is a vector
myfunc - function (p) {
   x[1] - p[1]
   for (i in c(2:length(p))) {
 x[i] - 0.8*p[i] + 0.2*p[i-1]
   }
   return (x)
}
Does this work at all? I get
myfunc - function (p) {
+x[1] - p[1]
+for (i in c(2:length(p))) {
+  x[i] - 0.8*p[i] + 0.2*p[i-1]
+}
+return (x)
+ }
myfunc(1:10)
Error in myfunc(1:10) : Object x not found
Anyway, simple loops are almost always avoidable. e.g.,
myfunc - function (p) {
   x - p
   x[-1] - 0.8 * p[-1] + 0.2 * p[-length(p)]
   x
}
Deepayan
That is, I'm calculating a time-weighted average. Unfortunately the
scale of the problem is big. length(p) in this case is such that each
call takes about 6 seconds, and I have to call it about 2000 times
(~3 hours). And, I'd like to do this each day. Thus, a more efficient
method is desirable.
Of course, this could be done faster by writing it in c, but I want
to avoid doing that if there already exists something internal to do
the operation quickly (because I've never programmed c for use in R).
Can anybody offer a solution?
I apologise if this is a naive question.
James
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html