Re: [R] difference between assignment syntax - vs =

2009-02-21 Thread Esmail Bonakdarian

Patrick Burns wrote:

'The R Inferno' page 78 is one source you can
look at.


Patrick Burns


wow .. nice! .. thanks for posting this reference.

Esmail

__
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] Sorting rows in a matrix based on vector of indecies

2009-02-20 Thread Esmail Bonakdarian

Hello

I have a matrix of size rows x cols.
I also have a vector  of size rows.

The vector contains index values that corresponds to rows in the matrix.

I would like to re-arrange/sort the contents of the matrix according
to the entries in the vector. Can this be done efficiently in R and
possibly in place? Or would it be better to allocate another whole matrix?

Simple example:

matrix:
ff
aa
zz
bb


vector:
2, 4, 1, 3

new matrix:
aa
bb
ff
zz


I suppose I could allocate another matrix and loop through the vector
of indecies placing entries into the new matrix. Do I have to worry
about deallocating the memory space of the first matrix or will it
automagically go away if I assign the new matrix to the old matrix
identifier (assuming nothing else is pointing at it)?

Thanks,
Esmail

__
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] Sorting rows in a matrix based on vector of indecies

2009-02-20 Thread Esmail Bonakdarian

Hi David,

This was useful, thanks.

The example was just that, there are no as, b,s etc I was
just trying to show what I was trying to do.


Using m[v,]

where m was the matrix in my example and v the vector of
index values works great as you suggested Thanks.

R is quite a powerful language as I am starting to discover
(and folks on the list here are very helpful).

Regards,

Esmail

David Winsemius wrote:


See if this helps:

  mtx-matrix( rep(letters[1:4], 6), nrow=4)
  mtx
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,] a  a  a  a  a  a
[2,] b  b  b  b  b  b
[3,] c  c  c  c  c  c
[4,] d  d  d  d  d  d

  mtx[c(2, 4, 1, 3), ]

 [,1] [,2] [,3] [,4] [,5] [,6]
[1,] b  b  b  b  b  b
[2,] d  d  d  d  d  d
[3,] a  a  a  a  a  a
[4,] c  c  c  c  c  c

You can do with that result whatever you want:

assign it,   m2 - mtx[c(2, 4, 1, 3), ]


__
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] Python and R

2009-02-19 Thread Esmail Bonakdarian

Doran, Harold wrote:

lm(y ~ x-1)
solve(crossprod(x), t(x))%*%y# probably this can be done more
efficiently



You could do

crossprod(x,y) instead of t(x))%*%y



that certainly looks more readable (and less error prone) to an R newbie
like myself :-)

__
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] Python and R

2009-02-19 Thread Esmail Bonakdarian

Hi Kenn,

Thanks for the suggestions, I'll have to see if I can figure out how to
convert the relatively simple call to lm with an equation and the data file
to the functions you mention (or if that's even feasible).

Not an expert in statistics myself, I am mostly concentrating on the
programming aspects of R. Problem is that I suspect my colleagues who
are providing some guidance with the stats end are not quite experts
themselves, and certainly new to R.

Cheers,

Esmail

Kenn Konstabel wrote:
lm does lots of computations, some of which you may never need. If speed 
really matters, you might want to compute only those things you will 
really use. If you only need coefficients, then using %*%, solve and 
crossprod will be remarkably faster than lm


# repeating someone else's example
# lm(DAX~., EuStockMarkets)

 y - EuStockMarkets[,DAX]
 x - EuStockMarkets
 x[,1]-1
colnames(x)[1] - Intercept

lm(y ~ x-1)
solve(crossprod(x), t(x))%*%y# probably this can be done more 
efficiently


# and a naive timing

  system.time( for(i in 1:1000) lm(y ~ x-1))
   user  system elapsed
  14.640.33   32.69
  system.time(for(i in 1:1000) solve(crossprod(x), crossprod(x,y)) )
   user  system elapsed
   0.360.000.36


Also lsfit() is a bit quicker than lm or lm.fit.

Regards,
Kenn


__
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] Python and R

2009-02-19 Thread Esmail Bonakdarian

Gabor Grothendieck wrote:

On Wed, Feb 18, 2009 at 7:27 AM, Esmail Bonakdarian esmail...@gmail.com wrote:

Gabor Grothendieck wrote:


See ?Rprof for profiling your R code.

If lm is the culprit, rewriting your lm calls using lm.fit might help.

Yes, based on my informal benchmarking, lm is the main bottleneck, the
rest
of the code consists mostly of vector manipulations and control structures.

I am not familiar with lm.fit, I'll definitely look it up. I hope it's
similar
enough to make it easy to substitute one for the other.

Thanks for the suggestion, much appreciated. (My runs now take sometimes
several hours, it would be great to cut that time down by any amount :-)



Yes, the speedup can be significant.  e.g. here we cut the time down to
40% of the lm time by using lm.fit and we can get down to nearly 10% if
we go even lower level:


Wow those numbers look impressive, that would be a nice speedup to have.

I took a look at the manual and found the following at the top of
the description for lm.fit:

  These are the basic computing engines called by lm used to fit linear
   models. These should usually not be used directly unless by experienced
   users. 

I am certainly not an experienced user - so I wonder how different it
would be to use lm.fit instead of lm.

Right now I cobble together an equation and then call lm with it and the
datafile.

I.e.,

LM.1 = lm(as.formula(eqn), data=datafile)
s=summary(LM.1)

I then extract some information from the summary stats.

I'm not really quite sure what to make of the parameter list in lm.fit

I will look on-line and see if I can find an example showing the use of
this - thanks for pointing me in that direction.

Esmail


system.time(replicate(1000, lm(DAX ~.-1, EuStockMarkets)))

   user  system elapsed
  26.850.07   27.35

system.time(replicate(1000, lm.fit(EuStockMarkets[,-1], EuStockMarkets[,1])))

   user  system elapsed
  10.760.00   10.78

system.time(replicate(1000, qr.coef(qr(EuStockMarkets[,-1]), 
EuStockMarkets[,1])))

   user  system elapsed
   3.330.003.34

lm(DAX ~.-1, EuStockMarkets)


Call:
lm(formula = DAX ~ . - 1, data = EuStockMarkets)

Coefficients:
 SMI   CAC  FTSE
 0.55156   0.45062  -0.09392


# They call give the same coefficients:



lm.fit(EuStockMarkets[,-1], EuStockMarkets[,1])$coef

SMI CACFTSE
 0.55156141  0.45062183 -0.09391815

qr.coef(qr(EuStockMarkets[,-1]), EuStockMarkets[,1])

SMI CACFTSE
 0.55156141  0.45062183 -0.09391815



__
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] Python and R

2009-02-18 Thread Esmail Bonakdarian

Gabor Grothendieck wrote:



See ?Rprof for profiling your R code.

If lm is the culprit, rewriting your lm calls using lm.fit might help.


Yes, based on my informal benchmarking, lm is the main bottleneck, the rest
of the code consists mostly of vector manipulations and control structures.

I am not familiar with lm.fit, I'll definitely look it up. I hope it's similar
enough to make it easy to substitute one for the other.

Thanks for the suggestion, much appreciated. (My runs now take sometimes
several hours, it would be great to cut that time down by any amount :-)

Esmail

__
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] Python and R

2009-02-17 Thread Esmail Bonakdarian

Hello all,

I am just wondering if any of you are doing most of your scripting
with Python instead of R's programming language and then calling
the relevant R functions as needed?

And if so, what is your experience with this and what sort of
software/library  do you use in combination with Python to be able
to access R's functionality.

Is there much of a performance hit either way? (as both are interpreted
languages)

Thanks,
Esmail

__
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] Python and R

2009-02-17 Thread Esmail Bonakdarian
Hello!

On Tue, Feb 17, 2009 at 5:58 PM, Warren Young war...@etr-usa.com wrote:

 Esmail Bonakdarian wrote:

 I am just wondering if any of you are doing most of your scripting
 with Python instead of R's programming language and then calling
 the relevant R functions as needed?

 No, but if I wanted to do such a thing, I'd look at Sage: http://sagemath.org/

ah .. thanks for the pointer, I had not heard of Sage, I was just
starting to look at
SciPy.

 It'll give you access to a lot more than just R.

 Is there much of a performance hit either way? (as both are interpreted
 languages)

 Are you just asking, or do you have a particular execution time goal, which 
 if exceeded
 would prevent doing this?  I ask because I suspect it's the former, and fast 
 enough is fast
 enough.

I put together a large'ish R program last year, but I think I would be
happier if I could code
it in say Python - but I would rather not do that at the expense of
execution time.

Thanks again for telling me about Sage.

Esmail

__
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] Python and R

2009-02-17 Thread Esmail Bonakdarian
On Tue, Feb 17, 2009 at 6:05 PM, Barry Rowlingson
b.rowling...@lancaster.ac.uk wrote:
 2009/2/17 Esmail Bonakdarian esmail...@gmail.com:

  When I need to use the two together, it's easiest with 'rpy'. This
 lets you call R functions from python, so you can do:

  from rpy import r
  r.hist(z)

wow .. that is pretty straight forward, I'll have to check out rpy for sure.

 to get a histogram of the values in a python list 'z'. There are some
 complications converting structured data types between the two but
 they can be overcome, and apparently are handled better with the next
 generation Rpy2 (which I've not got into yet). Google for rpy for
 info.

Will do!

 Is there much of a performance hit either way? (as both are interpreted
 languages)

  Not sure what you mean here. Do you mean is:

  R sum(x)

 faster than

 Python sum(x)

 and how much worse is:

 Python from rpy import r
 Python r.sum(x)


Well, I have a program written in R which already takes quite a while
to run. I was
just wondering if I were to rewrite most of the logic in Python - the
main thing I use
in R are its regression facilities - if it would speed things up. I
suspect not since
both of them are interpreted, and the bulk of the time is taken up by
R's regression
calls.

Esmail

__
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] how to randomly eliminate half the entries in a vector?

2009-02-17 Thread Esmail Bonakdarian

Hello all,

I need some help with a nice R-idiomatic and efficient solution to a
small problem.

Essentially, I am trying to eliminate randomly half of the entries in
a vector that contains index values into some other vectors.


More details:

I am working with two strings/vectors of 0s and 1s. These will contain
about 200 elements (always the same number for both)

I want to:

1. determines the locations of where the two strings differ

   -- easy using xor(s1, s2)

2. *randomly* selects *half* of those positions

   -- not sure how to do this. I suppose the result would be
   a list of index positions of size sum(xor(s1, s2))/2


3. exchange (flip) the bits in those random positions for both strings

   -- I have something that seems to do that, but it doesn't look
   slick and I wonder how efficient it is.

Mostly I need help for #2, but will happily accept suggestions for #3,
or for that matter anything that looks odd.

Below my partial solution .. the HUX function is what I am trying
to finish if someone can point me in the right direction.

Thanks
Esmail
--



rm(list=ls())


# create a binary vector of size len
#
create_bin_Chromosome - function(len)
{
  sample(0:1, len, replace=T)
}




# HUX - half uniform crossover
#
# 1. determines the locations of where the two strings
#differ (easy xor)
#
# 2. randomly selects half of those positions
#
# 3. exchanges (flips) the bits in those positions for
#both
#
HUX - function(b1, b2)
{
  # 1. find differing bits
  r=xor(b1, b2)

  # positions where bits differ
  different = which(r==TRUE)

  cat(\nhrp: , different, \n)
  # 2. ??? how to do this best so that each time
  #a different half subset is selected? I.e.,
  #sum(r)/2 positions.


  # 3. this flips *all* positions, should really only flip
  #half of them (randomly selected half)
  new_b1 = b1
  new_b2 = b2

  for(i in different)  # should contain half the entries (randomly)
  {
new_b1[i] = b2[i]
new_b2[i] = b1[i]
  }

  result - matrix(c(new_b1, new_b2), 2, LEN, byrow=T)
  result
}



LEN = 5
b1=create_bin_Chromosome(LEN)
b2=create_bin_Chromosome(LEN)

cat(b1, \n)
cat(b2, \n)

idx=HUX(b1, b2)
cat(\n\n)
cat(idx[1,], \n)
cat(idx[2,], \n)

__
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] how to randomly eliminate half the entries in a vector?

2009-02-17 Thread Esmail Bonakdarian
(sorry if this is a duplicate-problems with posting at my end)

Hello all,

I need some help with a nice R-idiomatic and efficient solution to a
small problem.

Essentially, I am trying to eliminate randomly half of the entries in
a vector that contains index values into some other vectors.

More details:

I am working with two strings/vectors of 0s and 1s. These will contain
about 200 elements (always the same number for both)

I want to:

1. determines the locations of where the two strings differ

-- easy using xor(s1, s2)

2. *randomly* selects *half* of those positions

-- not sure how to do this. I suppose the result would be
a list of index positions of size sum(xor(s1, s2))/2

3. exchange (flip) the bits in those random positions for both strings

-- I have something that seems to do that, but it doesn't look
slick and I wonder how efficient it is.

Mostly I need help for #2, but will happily accept suggestions for #3,
or for that matter anything that looks odd.

Below my partial solution .. the HUX function is what I am trying
to finish if someone can point me in the right direction.

Thanks
Esmail
--

rm(list=ls())


# create a binary vector of size len
#
create_bin_Chromosome - function(len)
{
   sample(0:1, len, replace=T)
}


# HUX - half uniform crossover
#
# 1. determines the locations of where the two strings
#differ (easy xor)
#
# 2. randomly selects half of those positions
#
# 3. exchanges (flips) the bits in those positions for
#both
#
HUX - function(b1, b2)
{
   # 1. find differing bits
   r=xor(b1, b2)

   # positions where bits differ
   different = which(r==TRUE)

   cat(\nhrp: , different, \n)
   # 2. ??? how to do this best so that each time
   #a different half subset is selected? I.e.,
   #sum(r)/2 positions.

   # 3. this flips *all* positions, should really only flip
   #half of them (randomly selected half)
   new_b1 = b1
   new_b2 = b2

   for(i in different)  # should contain half the entries (randomly)
   {
 new_b1[i] = b2[i]
 new_b2[i] = b1[i]
   }

   result - matrix(c(new_b1, new_b2), 2, LEN, byrow=T)
   result
}

LEN = 5
b1=create_bin_Chromosome(LEN)
b2=create_bin_Chromosome(LEN)

cat(b1, \n)
cat(b2, \n)

idx=HUX(b1, b2)
cat(\n\n)
cat(idx[1,], \n)
cat(idx[2,], \n)

__
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] how to randomly eliminate half the entries in a vector?

2009-02-17 Thread Esmail Bonakdarian

Gene Leynes wrote:

This is my first help post, hope it works!

Just check out the sample function
At the command line type:
?sample

I think it will be pretty clear from the documentation.


Yes, most excellent suggestion and quite helpful!

Thanks,
Esmail

__
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] running R-code outside of R

2008-06-25 Thread Esmail Bonakdarian

Jim Porzak wrote:
The user of your R script sees only the outputs you create. The R source 
is hidden.


Ah .. that sounds great .. I wish I had known about this a month ago!
I'll have to check it out - thanks!

Esmail



HTH,
Jim Porzak

..

   Would the R script that is being run be hidden from the user, or
   would the
   user be able to view/download/save the R source code - or could it
   be hidden
   so they just run the code, but never see it?

   Esmail


__
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] running R-code outside of R

2008-06-25 Thread Esmail Bonakdarian

Spencer Graves wrote:
 If you want to hide the fact that you are using R -- especially if 
you charge people for your software that uses R clandestinely -- that's 
a violation of the license (GPL).  


No on both accounts .. but thanks for pointing this out none the less.

__
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] Programming Concepts and Philosophy

2008-06-20 Thread Esmail Bonakdarian

hadley wickham wrote:

2008/6/20 [EMAIL PROTECTED] [EMAIL PROTECTED]:

If you do nothing to your code, in 18 months time its performance will
have doubled because computers will have become faster.  Your code
will not get easier to understand by itself.


Very nicely put .. and true too!

__
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] replace column headers

2008-06-17 Thread Esmail Bonakdarian

Paul Adams wrote:



Hello everyone,=I have a question as to how to remove the column headers
in a data file and then replace those with titles from another file in this case
the file labeled ann  (
in which the titles are all in one column).


Maybe this will help partially.

I am not sure on how to read your new column names from a file into a vector
but if you can get them into that format this should let you accomplish your
goal.

Esmail



 load(Data/simpleTestData2.rda)

 df
   Y X1   X2   X3 X4 X5 X6
1   74.9 10 49.8  0.2 99 50 57
2   79.8 11 49.6  0.4 69 91 57
3   84.4 12 48.8  1.2 30 38 58
4   88.8 13 47.6  2.4 77 87 58

 colnames(df)
[1] Y  X1 X2 X3 X4 X5 X6

 altnames=c(A, B1, B2, B3, B4, B5, B6)

 colnames(df)=altnames

 df
   A B1   B2   B3 B4 B5 B6
1   74.9 10 49.8  0.2 99 50 57
2   79.8 11 49.6  0.4 69 91 57
3   84.4 12 48.8  1.2 30 38 58
4   88.8 13 47.6  2.4 77 87 58

 save(df, file=example.rda)

# new file will contain the new column headers.

__
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] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian

Hi,

I have matrix of bits and a target vector. Is there an
efficient way to search the rows of the matrix for the target?
I am interested in the first row index where target is found.

Example:

 source(lookup.R)
  [,1] [,2] [,3] [,4] [,5]
 [1,]10110
 [2,]11010
 [3,]00100
 [4,]10011
 [5,]10111
 [6,]11001
 [7,]10011
 [8,]00111
 [9,]01101
[10,]00010

target:  1 1 0 1 1

Should return -1 (or some other indicator) since the
target was not found in any of the rows.



 source(lookup.R)
  [,1] [,2] [,3] [,4] [,5]
 [1,]00110
 [2,]10000
 [3,]10000
 [4,]11000
 [5,]11100
 [6,]00110
 [7,]01110
 [8,]00110
 [9,]11011
[10,]10100

target:  1 1 0 1 1

Should return 9 since the target was  found in row 9


If the target is found, it is no longer necessary to keep
searching the rest of the matrix (which may be quite large)

The data/size etc may change of course, but target will
always have the same number of columns as the matrix.

I tried variations of which, and a for loop
comparing pop[i,] to target without much success, nor
did google yield any results. I am hoping someone here
can provide a suggestion.

Thanks,

EB


-

# Here is the code that generates the above data

create_bin_string - function(len)
{
  sample(0:1, len, replace=T)
}

ROWS = 10
COLS =  5
pop = matrix(create_bin_string(ROWS*COLS), ROWS, COLS, byrow=T)



target=c(1, 1, 0, 1, 1)

# my population
print(pop)

# I am looking for the index of this in pop
# if present (else -1?)
cat(\ntarget: , target, \n)



##
## this is NOT working
## plus it would continue to search
## after it found the target
##
for(i in ROWS)
   if (pop[i,] == target)
  cat(\nfound in row: , i, \n\n)

__
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] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian

Henrique Dallazuanna wrote:

Try this:

which(apply(t(m) == target, 2, all))


Wow! .. talk about concise! Neat! Thanks.

This will return all matches correct? So if I only wanted
the first I'd simply subscript [1] into it.

Do you think the fact that it searches the whole matrix instead
of stopping when it finds a match may slow it down?

This is my own solution I came up with in the meantime,
looks rather pedestrian compared to your one line, but it will
drop out immediately once if finds the target. Yours looks (based
simply on appearance :-) faster. Having no feel for the language
I may just have to time them.


I would assume that your solution would be faster simple since it's
using built-in language constructs which are optimized (and implemented
in C?) instead of my own interpreted way.


# return index of target in pop, else -1
searchPop - function(pop, target)
{
rows = length(pop[1,])
for(i in 1:rows)
{
result = (pop[i,] == target)
if (sum(which(result==FALSE)) == 0)
return(i)
}

return (-1)
}


idx=searchPop(pop, target)

if (idx  0)
{
cat(NOT found\n)
} else
cat(Found at position , idx, \n)



Esmail

--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O


__
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] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian

Dimitris Rizopoulos wrote:

try this:

match.pat - function (mat, target, nomatch = -1) {
   f1 - do.call(paste, c(as.data.frame(mat), sep = \r))
   f2 - paste(target, collapse = \r)
   ind - f1 %in% f2
   if (any(ind)) which(ind)[1] else nomatch
}


Thanks! More R for me to sink my teeth in :-)  My own solution
doesn't seem to work quite correctly as I found out from some
further testing .. so the solutions posted here are much appreciated!

Esmail

__
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] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian



# determine which data matches
matches - t(pop) == target  # 't' due to matching in column order

# colSums equal to COLS will indicate matches
which(colSums(matches) == COLS)




Neat! .. somewhat similar to the solution I came up with in the
meantime, only yours works :-)

Thanks Jim.

Esmail

__
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] Adding new columns to (output) data - e.g., read 5 cols write 8

2008-06-11 Thread Esmail Bonakdarian

Hello, I have the following task I'd like to accomplish:

A file contains 5 columns of data (several hundred rows), let's call
them a, b, c, d and e (ie these are their column headers)

I also have a set of definitions, e.g.,

f = a + b
g = a * 3
h = c + d
etc.

I would like to write out a new .rda file that contains columns

a b c d e f g h etc.

I.e. , the original data plus new columns (with headers and data).

It seems that there ought to be a simple way to do this, could
someone provide some guidance on the best way to accomplish
this task? (I tried a few things as this seemed rather trivial,
but did not succeed. I still hope/assume this is a trivial thing
to do if one knows R well)

Thanks,

Esmail

ps: I want to thank everyone again who posted their solutions
to my previous query. Seeing different solutions for the same
problem is a tremendously effective way to learn something
new.

__
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] Adding new columns to (output) data - e.g., read 5 cols write 8

2008-06-11 Thread Esmail Bonakdarian

Hi Erik,

Erik Iverson wrote:

Esmail -

Are these 5 vectors of data stored in a data.frame?  I assume so.


Yes, I do a simple load() call first to read the .rda file ...


test2 - transform(test, d = 2*a + b, e = 3*c)
save(test2, file = test2.Rdata)

Does this help?


Yes it does .. this is just what I needed. I'll probably do it
in this way:

df=transform(df, X5=X1+X2)
df=transform(df, X6=X1-X2)
etc ...


building it up the df line by line since the definitions have been
provided one per line (instead of queuing them up in one line like
you showed). I'll have to write a  little awk or ruby script to take
the original definitions and wrap them into the transform call, but then
I should be all set to go.

Thanks again!

Esmail

__
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] Adding new columns to (output) data - e.g., read 5 cols write 8

2008-06-11 Thread Esmail Bonakdarian

jim holtman wrote:

yourDF - cbind(yourDF, f=yourDF$a+yourDF$b, g=yourDF$a * 3,
h=yourDF$c + yourDF$d)


Thanks Jim, I also learned about the transform() method from Erik
which will also work beautifully.

Esmail

__
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] R + Linux

2008-06-06 Thread Esmail Bonakdarian

steven wilson wrote:


I'm planning to install Linux on my computer to run R (I'm bored of
W..XP). However, I haven't used Linux before and I would appreciate,
if possible, suggestions/comments about what could be the best option
install, 


Hi,

I have used Linux since the early 1990s starting with the original
slackware distribution, followed by various versions of Red Hat,
Gentoo (compiled from source), Fedora and now Ubuntu.

Ubuntu is my choice for having the least troublesome install and
maintenance. It has a very nice package manager, and if your goal
is to *use* a Linux system rather than tinker with it, you could
do much worse than Ubuntu.

I installed R via the package manger a month ago or so, very easy
and trouble free.

Hope that helps,

Esmail

__
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] R + Linux

2008-06-06 Thread Esmail Bonakdarian

FWIW, those who are curious about Linux but are not willing
or ready to abandon the Windows platform can now very easily
try out Ubuntu without having to repartition their hard drive.

Wubi is a project that installs Ubuntu under Windows so that it
can be uninstalled easily and requires no messing around with
hard drive partitions.

From the Wubi web site:

Wubi is an officially supported Ubuntu installer for Windows users that can 
bring you to the Linux world with a single click. Wubi allows you to install 
and uninstall Ubuntu as any other Windows application, in a simple and safe 
way. Are you curious about Linux and Ubuntu? Trying them out has never been 
easier!



For more information see:

  http://wubi-installer.org/

Esmail

__
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] Improving data processing efficiency

2008-06-06 Thread Esmail Bonakdarian

hadley wickham wrote:




Hi,

I tried this suggestion as I am curious about bottlenecks in my own
R code ...


Why not try profiling?  The profr package provides an alternative
display that I find more helpful than the default tools:

install.packages(profr)


 install.packages(profr)
Warning message:
package ‘profr’ is not available


any ideas?

Thanks,
Esmail



library(profr)
p - profr(fcn_create_nonissuing_match_by_quarterssinceissue(...))
plot(p)

That should at least help you see where the slow bits are.

Hadley



__
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] Improving data processing efficiency

2008-06-06 Thread Esmail Bonakdarian

Esmail Bonakdarian wrote:

hadley wickham wrote:




Hi,

I tried this suggestion as I am curious about bottlenecks in my own
R code ...


Why not try profiling?  The profr package provides an alternative
display that I find more helpful than the default tools:

install.packages(profr)


  install.packages(profr)
Warning message:
package ‘profr’ is not available


I selected a different mirror in place of the Iowa one and it
worked. Odd, I just assumed all the same packages are available
on all mirrors.

__
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] Thank you

2008-05-29 Thread Esmail Bonakdarian

Tubin wrote:

In the past few weeks I have had to give myself a crash course in R, in order
to accomplish some necessary tasks for my job.  During that time, I've found
this forum to be helpful time and time again - usually I find the answer to
my problem by searching the archives; once or twice I've posted questions
and been given near-immediate solutions.

I just wanted to thank the forum participants for all of their contributions
over the years!  



Hear hear! .. I've benefited greatly from reading the postings here
and some of the members have been very generous with their knowledge too!

Esmail

__
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] How do you exit a function in R?

2008-05-29 Thread Esmail Bonakdarian

Bill Cunliffe wrote:

For example, based on a certain condition, I may want to exit my code early:

 


# Are there the same number of assets in prices and
positions?

if (nAssetPositions != nAssetPrices) {

cat(Different number of assets! \n\n)

exit function



I've wondered about that myself

Have you tried  return() ? It would return NULL I think, as long as that
doesn't cause a problem it should exit the method.

__
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] hash or other quick lookup function?

2008-05-28 Thread Esmail Bonakdarian

Hi Duncan,

Duncan Murdoch wrote:

Duncan Murdoch wrote:

Esmail Bonakdarian wrote:
 

Hello all,

I have a matrix of bit values.

I compute certain values based on the bits in each row. There may be
*duplicate* entries in the matrix, ie several rows may be identical.
These rows change over time, and identical entries may not be next to
each other.

Computing these values is time consuming, so I would like a way to 
store a value once it's computed. That way I would be able to look up the value
based on a given bitstring (row in the matrix) and avoid having to 
re-compute the same value.


So, my thought is a hash function based on a bit string - does R have such
a thing? Or is there an alternative approach that would do the same in R?
The lookup should be quick, otherwise the gains from avoiding
recomputing identical values would be lost to some extent.


Environments can be hashed. To use this, you'd convert the bit string 
into a character string, and use that as the name of a variable in an 
environment.


For example,

cache - new.env(hash=TRUE, parent=emptyenv())
 ...
bits - '0101'
if (exists(bits, cache)) {
  return(get(bits, cache))
} else {
  value - long.computation()
  assign(bits, value)
  


Whoops, that assignment should have been in the cache:

   assign(bits, value, envir=cache)

  return(value)
}



Thank you for posting this, I'll have to give it a try.
If I can get this to work it should save some some time
spent on recomputing values.

(Sorry for the delayed reply, I was traveling for the
whole last day - suffering from some reverse jet lag at
the moment :-)

Can anyone else think of other alternatives? Always happy
to learn something new.

Thanks!

Esmail

ps: do you think the conversion to a character string
before comparison/search will cause a noticeable
performance hit? I still lack an intuitive feel for
R the programming language. I'd have more of an idea
with regular high-level programming languages.



Duncan Murdoch


__
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] How to make R running faster

2008-05-28 Thread Esmail Bonakdarian

Neil Shephard wrote:


Loops are not massively efficient within R.

Look into using the apply() family of functions
(eapply()/lapply()/mapply/rapply()/tapply()).


Didn't someone post not too long ago that apply is
internally represented as a for-loop? Or am I not
remembering this correctly?

The reason for apply was that it was more concise but not
necessarily more efficient.

Esmail

__
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] recompute values repeatedly, or new file for glm()?

2008-05-20 Thread Esmail Bonakdarian

Hello all,

I need to tap into the collective wisdom of the group re an issue of
efficiency.

A sketch of the situation:

Let's say 4000 observations in variables Y, X1, X2 , X3 and X4.

I would like to feed various combinations of this expression

Y ~ X1+X2+X3+X4 + I(X1^2)+I(X2^2)+I(X3^2)+I(X4^2) + X1*X2 + X1*X3 + X1*X4 + 
X2*X3 + X2*X4 + X3*X4


repeatedly to glm(). (I really have little knowledge about how R or
glm() works internally)

Let's say I call glm() 200 times with various combinations does it
make sense to compute these various factors based on X1 .. X4 and
store them in a file along with the original data, and then use
that file for the glm() calls or will the overhead of computing
these factors be so small that it's not worth computing these
values ahead of time and storing them in a file?

This is simplified example, I actually have 20 original variables
rather than the 4 I show above. I hope this made some sense.

Thanks,
Esmail

ps: If it makes sense to preprocess X1,X2,X3 and X4 to generate a new
file that contains the values for

X1, X2, X3, X4, I(X1^2), I(X2^2), I(X3^2), I(X4^2), X1*X2, X1*X3, X1*X4 
,X2*X3, X2*X4, X3*X4


is there an easy way to take the expression at the top of the message
and convert the values in the original dataframe and compute them so that
I can write them out to a new file?

__
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] Printing output in STDOUT

2008-05-20 Thread Esmail Bonakdarian

Edward Wijaya wrote:

Hi,

Currently the R script I have is executed with this command:

$ R CMD BATCH mycode.R

And the output is stored in mycode.Rout.

Is there a way I can issue command from shell (like above)
so that the output is printed to STDOUT?

It's  troublesome to open the Rout file every time to debug.


Under a Unix system you could try to pipe the command into tail -f

i.e.,

$ R CMD BATCH mycode.R | tail -f

That should display the file as it gets written.

I don't have access to a Unix system right now to give this a try
but it should be a work around until someone who knows more about
R can come up with an answer.

HTH

Esmail

__
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] Printing output in STDOUT

2008-05-20 Thread Esmail Bonakdarian

Prof Brian Ripley wrote:

On Tue, 20 May 2008, Esmail Bonakdarian wrote:


Edward Wijaya wrote:

Hi,

Currently the R script I have is executed with this command:

$ R CMD BATCH mycode.R

And the output is stored in mycode.Rout.

Is there a way I can issue command from shell (like above)
so that the output is printed to STDOUT?

It's  troublesome to open the Rout file every time to debug.


Under a Unix system you could try to pipe the command into tail -f

i.e.,

$ R CMD BATCH mycode.R | tail -f

That should display the file as it gets written.


Buffering may get in the way -- so 'gets written' may be much later than 
when it is output by R.


Yes, that's quite likely to be the case .. I like your suggestions
below much better - saved away for future reference too.

Esmail

__
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] Format integer

2008-05-13 Thread Esmail Bonakdarian

Anh Tran wrote:

Hi,
What's one way to convert an integer to a string with preceding 0's?
such that
'13' becomes '013'
to be put into a string

I've tried formatC, but they removes all the zeros and replace it with
blanks


Hi,

try sprintf:

i=13
 cat(sprintf(%05d\n, i))
00013


HTH,

Esmail

__
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] test

2008-05-13 Thread Esmail Bonakdarian

Tony Plate wrote:
You probably should check this section in your R-help subscription 
options (via https://stat.ethz.ch/mailman/options/r-help/, I think):



Receive your own posts to the list?




Tony,

Like jt I too have it set to receive my own messages, but I too
don't see them. I wonder if it has to do with the fact that both
of us use gmail to post to the list?

In any case, regardless if we see them, they are getting posted,
which is what matters :)

Cheers,
Esmail

__
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] test

2008-05-13 Thread Esmail Bonakdarian

Charilaos Skiadas wrote:

On May 13, 2008, at 5:52 AM, Esmail Bonakdarian wrote:


Tony Plate wrote:
You probably should check this section in your R-help subscription 
options (via https://stat.ethz.ch/mailman/options/r-help/, I think):

Receive your own posts to the list?




Tony,

Like jt I too have it set to receive my own messages, but I too
don't see them. I wonder if it has to do with the fact that both
of us use gmail to post to the list?


Bingo! Well, I don't know if this happens with the web interface to 
gmail, but if you use POP to access your gmail account, then any emails 
you send to any kind of list will not get back to you 


Yup, I use Thunderbird to read/process my gmail :-)

(Problem being, 
sort of, that gmail groups things in Conversations, and in this case 
will remember the email you sent out and use that copy in the 
conversation, instead of the one sent through the list. 


Except that part of the conversation sits in my sent folder rather
than being nicely threaded into the conversation.

Not a very good 
excuse in my opinion, and a particularly irritating feature.).


agreed!

In the rare occasions  where my waiting powers get exhausted before 
someone on this very helpful list replies to my email, I just use the 
online archives to check whether my email was sent or not.


Yes, I did that initially when I wasn't sure if my message was getting
out. Often thought I don't have to do this because the helpful folks on
this list have already replied to my posting.

Best,
Esmail

__
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] Random number generation

2008-05-13 Thread Esmail Bonakdarian

Greg Snow wrote:

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Esmail Bonakdarian
Sent: Sunday, May 11, 2008 7:25 AM
To: Prof Brian Ripley
Cc: [EMAIL PROTECTED]
Subject: Re: [R] Random number generation


[snip]


What I read doesn't seem to be incorrect however (it may even
have been an archived message here), the *language* itself
does not seem to support block *comments*. Using conditional
constructs, or an IDE/editor to achieve similar results is a
work around - but not the same. I don't mean to nitpick, but
as a computer scientist I see this as different :-)


I am not a computer scientist, so correct me if I am wrong, but from what I remember (and a quick glance at my copy of Kernighan Ritchie), the C *language* itself does not support block *comments*, rather the preproccessor 


Hi there,

Yes, while that may be true, many other languages that don't use an
explicit pre-processor mechanism (though internally will go
through a similar process) do support block comments. It's a
moot point anyway, since when you compile a C program the preprocessor
also runs (just like the linker etc) unless you specifically ask for
something differently via command line switches. As far as the
programmer is concerned the semantics are those of a language
that provides block comments with the simple use of /* */ , and
not dependent on if-statements or editor/IDE tricks.

Since R is optimized for interactive use rather than compilation, running everything through a preproccessor is not really an option.  However as an additional work around 


My use then may be not typical since I am trying to write scripts in R
and only use R directly to try out functions/language constructs.

you could always run your R scripts through the C preproccessor and 

 have it strip the block comments for you.

Too much work, call me old school, but I like the computer do work for me,
rather than the other way around :-)


Given the complexity of implementing block commenting (even deciding on the 
syntax) and the ease of current work arounds, the cost benefit ratio probably 
puts this very near the bottom of the priority list.


I couldn't possibly offer an opinion on that .. I'll happily defer to you
and the other experts here for this.

Cheers,
Esmail

__
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] [OT] xemacs on windows vista

2008-05-12 Thread Esmail Bonakdarian

Wensui Liu wrote:

Hi, dear all,
I just switch to vista (ultimate) and have heard there is some problem
for the installation of xemacs on vista. Is there any insight or
experience that you could share? I really appreciate any input.
thank you so much!


Hi,

I don't know about XEmacs, but I am using a specially bundled
version of GNU Emacs by Vincet Goulet which is great. I was
using a plain-vanilla version of GNU Emacs before, but this one
has ESS  and spell check support built in :-)

I am using it under XP .. I believe this also works under Vista

Check out:

   http://vgoulet.act.ulaval.ca/en/ressources/emacs/

HTH,

Esmail

__
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] Random number generation

2008-05-11 Thread Esmail Bonakdarian

Stephan Kolassa wrote:


Have you tried successively removing/commenting parts of the script 
before the sample() command until the problem goes away? That way you 
should be able to pinpoint the offending script command.


Hi,

This brings up a question I have .. is there a way to do *block* comments
with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment
more than just one line at a time.

From what I have read this is not possible in R (at least not easily), but
I am eager for someone to contradict me :-)

Thanks,
Esmail

__
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] Random number generation

2008-05-11 Thread Esmail Bonakdarian

Hello there,

Prof Brian Ripley wrote:

On Sun, 11 May 2008, Esmail Bonakdarian wrote:


Stephan Kolassa wrote:


Have you tried successively removing/commenting parts of the script 
before the sample() command until the problem goes away? That way you 
should be able to pinpoint the offending script command.


Hi,

This brings up a question I have .. is there a way to do *block* comments
with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment
more than just one line at a time.

From what I have read this is not possible in R (at least not easily), 
but

I am eager for someone to contradict me :-)


if(FALSE) {
...
}

Any good editor can do block commenting, e.g. ESS.

You didn't tell us what you read, but I have never seen this in a 
reputable source.


I don't remember the source as I was reading widely all over the place
trying to get up to speed with R in a hurry  (having found this group
was one of the best sources).

What I read doesn't seem to be incorrect however (it may even have been an
archived message here), the *language* itself does not seem to support block
*comments*. Using conditional constructs, or an IDE/editor to achieve similar
results is a work around - but not the same. I don't mean to nitpick, but
as a computer scientist I see this as different :-)

I'll have to look at ESS though.

Thanks,
Esmail

ps: Ah, I seem Meta-; works as a toggle in emacs/ESS .. thanks for encouraging
me to look at that some more.

__
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] which.max2()

2008-05-09 Thread Esmail Bonakdarian

Hello,

which.max() only returns one index value, the one for the
maximum value. If I want the two index values for the two
largest values, is this a decent solution, or is there a
nicer/better R'ish way?

max2 -function(v)
{
m=which.max(v)
v[m] = -v[m]
m2=which.max(v)
result=c(m, m2)
result
}

Seems to work ok.

Thanks,
Esmail

__
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] which.max2()

2008-05-09 Thread Esmail Bonakdarian

Dimitris Rizopoulos wrote:

try this:

v - rnorm(10)
v
order(v, decreasing = TRUE)[1:2]


Wow .. that is slick! First I thought, wait .. I don't want to
reorder the elements, but this doesn't - it just returns the index
values in order. I don't really get that from reading the documentation,
it's probably there, but not that clear to me.

Thanks for showing me something more R'ish.

Esmail

__
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] which.max2()

2008-05-09 Thread Esmail Bonakdarian

Marc Schwartz wrote:


I might be tempted to take a more generic approach, where one can
provide an argument to the function to indicate that I want the 'top x'
maximum values and to give the user the option of returning the indices 
or the values themselves.


Perhaps:

which.max2 - function(x, top = 1, values = FALSE)
{
  if (values)
rev(sort(x))[1:top]
  else
order(x, decreasing = TRUE)[1:top]
}


Very cool too! .. Thanks Marc. Again, I did not get this from the
order documentation (ie that it manipulate index values rather than
the values themselves). Great to see examples.

Best,
Esmail

__
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] building a formula string bit by bit ..

2008-05-06 Thread Esmail Bonakdarian

Hello,

Still a newbie with R, though I have learned a lot from reading
this list. I'm hoping someone can help with this question:

I have two vectors, one for variables, and one for bits.

I want to build a string (really a formula) based on the values in my
vector of 1s and 0s in bits. If I have a one, I want to include the
corresponding entry in the vars vector, otherwise ignore it. Of course
the bits vector will not stay the same.

So for example:

  vars=c('X.1', 'X.2', 'X.3', 'X.4', 'X.5')

  bits=c(1, 0, 1, 1, 0)
  ones=which(bits==1)

should yield:

  X.1 + X.3 + X.4

where as

  bits=c(1, 1, 0, 0, 0)

would yield

  X.1 + X.2

the which operator gives me the index values, is there an easy and
*efficient* way to build this string so that I can pass it on to glm?
I can probably hack some ugly code together to do this, but it won't
be efficient, and it won't be elegant :-)

Can anyone help?

Thanks!

__
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] building a formula string bit by bit ..

2008-05-06 Thread Esmail Bonakdarian

Jorge Ivan Velez wrote:

Hi Esmail,

Try this:

vars=c('X.1', 'X.2', 'X.3', 'X.4', 'X.5')
bits=c(1, 0, 1, 1, 0)
paste(vars[which(bits==1)],collapse=+)

HTH,

Jorge


Wow .. that is beautiful :-) .. and exactly what I was looking
for (and suspected existed).

I ended up doing this:

   eqn=(paste(vars[which(bits==1)],collapse= + ))
   eqn=paste(c(Y.data ~), eqn)
   GLM.9 - glm(as.formula(eqn) , family=gaussian(identity), data=simpleData)

For some reason I couldn't collapse the two eqn/paste statements into one
statement but this seems to work.

Thank again Jorge and everyone else .. this group is a big help.

Esmail

__
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] Automatically generating new column names (and columns)

2008-05-06 Thread Esmail Bonakdarian

Once again I need to tap into the collective knowledge here.

Let's say I have the following columns and data below

Y X1 X2 X3 X4

I would like to generate additional new columns and column names
(ie the data would be squared - and I'd like the column names to
reflect this) like:

Y X1 X2 X3 X4  X1^2 X2^2 X3^2 X4^2


I believe I can compute the values correctly with the code below, but
I am unable to generate the column names. (This is a short example, in
reality I have many more columns and more complicated names)

for(i in 2:5)
{
   name=get(paste(names(simpleData)[i],^2, sep=))
   simpleData- transform(simpleData, name=(simpleData[,i]^2))
}

print(simpleData)

Any suggestions/hints .. I've searched the FAQ and web (this is how I
came across the get() function) but no luck so far.

Thanks,
Esmail

__
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] efficiency profiling? (was: Why R is 200 times slower than Matlab ?)

2008-04-30 Thread esmail bonakdarian

This has been an interesting discussion, and brings up two questions
for me:

Is there a good collection of hints/suggestions for R language idoms in terms
of efficiency? For instance I read not to use for-loops, so I used apply only to
later read that apply is internally implemented as a for so nothing gained
here. Warnings about pitfalls (such as nested loops), hints, suggestions would
be great.

The second question - is there some sort of profiling tool available that would
make it easy to recognize where the script is spending most of its time? Might
be especially useful for newbies like me.

Thanks all,

Esmail

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

_
Spell a grand slam in this game where word skill meets World Series. Get in the 
game.

08
[[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.


Re: [R] efficiency profiling?

2008-04-30 Thread esmail bonakdarian


 See ?Rprof for the tool.  For the tips, I think you just need to hang 
 around here a while.  I don't know of a nice collection (but I'm sure 
 there are several.)
 
 Duncan Murdoch


Hi,

thanks .. several folks pointed me to Rprof, I'll take a look.

Yes, I have been reading the list, the amount of messages per day
is simply amazing, I can hardly keep up. Do most of you read this
on the web or get it as digest? I am getting them as individual
e-mails (thank god for filters) ... :-)

Esmail

_
Back to work after baby–how do you know when you’re ready?

5797498ocid=T067MSN40A0701A
[[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.


Re: [R] Documentation General Comments

2008-04-21 Thread esmail bonakdarian





 I realize the R developers are probably overwhelmed and have little time
 for this, but the documentation really needs some serious reorganizaton.
 A good through description of basic variable types would help a lot,
 e.g. the difference between lists, arrays, matrices and frames. 

Agreed, esp since I am right now at this moment struggling with the
differences of lists, arrays and vectors. (I have two vectors that I am
trying to return from a function as an array of two rows - for some reason 
that's not working)

I just started with the R last week , and I am trying to get up to
speed with this language. And while I have many years
of experience with various programming languages, many which
I have learned on my own, I am encountering a number of things
that are slowing me down. I have downloaded the various intro/reference
materials.

I am super-happy to have found this list though, everyone seems
friendly and helpful, and I am learning a lot from it. Any new/additional
documentation would be much welcome.

_


esh_getintouch_042008
[[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.


Re: [R] Documentation General Comments

2008-04-21 Thread esmail bonakdarian
Thank you Rob, I just downloaded it and it looks very useful.

In the meantime I think I solved my immediate problem (and while
pluggin' away also deepened my understanding - or so I will at least
claim :-)

Esmail
 
 I don't know if you will find this helpful, but one of the better online 
 chapters on R Data Objects that I've found is by Tomas Aragon
 and Wayne Enanoria.  Among other nice attributes it has some tables that 
 summarize improtant functions for working with different data objects.
 The URL is:
 http://www.medepi.net/epir/epir_chap02.pdf
 
 
 Rob Baer


_
Pack up or back up–use SkyDrive to transfer files or keep extra copies. Learn 
how.

sh_skydrive_packup_042008
[[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] representing binary data for Genetic Algorithm in R

2008-04-20 Thread esmail bonakdarian
Hello all,

I am trying to implement a simple Genetic Algorithm. I am doing this
in R since I want access to the statistical functions (eg lm) it
provides.

I'm not new to programming, or GAs, but I am totally new to R (the
package and the language), and I am hoping someone could help with
these questions:

1. I am going to use a binary representation, it seems that vectors of
   Factors would be what I should use? I would specify values of
   TRUE/FALSE instead? Or is there a better choice for manipulating
   binary strings? Does this seem like a legitimate way to create a
   random one: sample(0:1, vec_size, replace=T)

2. Am I correct in assuming that R is interpreted (and hence is going to
   be noticeably slower than compiled languages)?

3. Is there some sort of debugging facility? 

4. Does anyone know how to do an effective search for R in google (or
   other search engines). The fact that this is a single letters seems
   to have most applications ignore this input. This might help me in
   finding answers to some other basic questions I have (such as is
   there an equivalent function to printf in R? cat and print
   are not quite working right for me -- but I need to dig deeper into
   the documentation)

I am not sure if this belongs into the help or development group, so
I'm giving this a try. If this is the wrong group to post in, please
let me know and I'll repost.

There will probably be more elementary questions (I am reading the
various manuals too .. but if anyone has some other favorite sites
they want to recommend please do so)

Thanks,
Esmail

ps: I there a USENET group dedicated to R?

pps: I am also exploring ways of calling R functions from Java, if
 anyone has any comments regarding that please share.
_
Going green? See the top 12 foods to eat organic.

1N1653A
[[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.


Re: [R] representing binary data for Genetic Algorithm in R

2008-04-20 Thread esmail bonakdarian




Hello!

 Dear Esmail,
 
 you really have to have a look at some introduction to R (e.g. 
 http://cran.r-project.org/doc/manuals/R-intro.pdf), but see the Manual 
 section in R website (http://www.r-project.org/). That would answer many 
 of your questions.

point well taken, I am a bit in a hurry with a project I inherited unexpectedly,
so I was trying to take some shortcuts in trying to get up to speed intending to
dig into the documentation more in depth as soon as possible. I do have the
documents you mention and have been trying to scan them.

thanks for your post,

Esmail

_
Going green? See the top 12 foods to eat organic.

1N1653A
[[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.