Re: [R] A More efficient method?

2007-07-04 Thread Ken Knoblauch
Keith Alan Chamberlain Keith.Chamberlain at Colorado.EDU writes:
 Cat=c('a','a','a','b','b','b','a','a','b')# Categorical variable
 C1=vector(length=length(Cat)) # New vector for numeric values

 for(i in 1:length(C1)){
   if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
 }
 
 C1
 [1] -1 -1 -1  1  1  1 -1 -1  1
 Cat
 [1] a a a b b b a a b

 ifelse(Cat == a, -1, 1)
[1] -1 -1 -1  1  1  1 -1 -1  1

HTH

__
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
and provide commented, minimal, self-contained, reproducible code.


[R] A More efficient method?

2007-07-04 Thread Keith Alan Chamberlain
Dear Rhelpers,

Is there a faster way than below to set a vector based on values from
another vector? I'd like to call a pre-existing function for this, but one
which can also handle an arbitrarily large number of categories. Any ideas?

Cat=c('a','a','a','b','b','b','a','a','b')  # Categorical variable
C1=vector(length=length(Cat))   # New vector for numeric values

# Cycle through each column and set C1 to corresponding value of Cat.
for(i in 1:length(C1)){
if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
}

C1
[1] -1 -1 -1  1  1  1 -1 -1  1
Cat
[1] a a a b b b a a b

Sincerely,
KeithC.
Psych Undergrad, CU Boulder (US)
RE McNair Scholar

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread ONKELINX, Thierry
Cat - c('a','a','a','b','b','b','a','a','b')
C1 - ifelse(Cat == 'a', -1, 1)



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
[EMAIL PROTECTED]
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney

 

 -Oorspronkelijk bericht-
 Van: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] Namens Keith Alan 
 Chamberlain
 Verzonden: woensdag 4 juli 2007 15:45
 Aan: r-help@stat.math.ethz.ch
 Onderwerp: [R] A More efficient method?
 
 Dear Rhelpers,
 
 Is there a faster way than below to set a vector based on 
 values from another vector? I'd like to call a pre-existing 
 function for this, but one which can also handle an 
 arbitrarily large number of categories. Any ideas?
 
 Cat=c('a','a','a','b','b','b','a','a','b')# Categorical variable
 C1=vector(length=length(Cat)) # New vector for numeric values
 
 # Cycle through each column and set C1 to corresponding value of Cat.
 for(i in 1:length(C1)){
   if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
 }
 
 C1
 [1] -1 -1 -1  1  1  1 -1 -1  1
 Cat
 [1] a a a b b b a a b
 
 Sincerely,
 KeithC.
 Psych Undergrad, CU Boulder (US)
 RE McNair Scholar
 
 __
 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
 and provide commented, minimal, self-contained, reproducible code.


__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread Benilton Carvalho
C1 - rep(-1, length(Cat))
C1[Cat == b]] - 1

b

On Jul 4, 2007, at 9:44 AM, Keith Alan Chamberlain wrote:

 Dear Rhelpers,

 Is there a faster way than below to set a vector based on values from
 another vector? I'd like to call a pre-existing function for this,  
 but one
 which can also handle an arbitrarily large number of categories.  
 Any ideas?

 Cat=c('a','a','a','b','b','b','a','a','b')# Categorical variable
 C1=vector(length=length(Cat)) # New vector for numeric values

 # Cycle through each column and set C1 to corresponding value of Cat.
 for(i in 1:length(C1)){
   if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
 }

 C1
 [1] -1 -1 -1  1  1  1 -1 -1  1
 Cat
 [1] a a a b b b a a b

 Sincerely,
 KeithC.
 Psych Undergrad, CU Boulder (US)
 RE McNair Scholar

 __
 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
 and provide commented, minimal, self-contained, reproducible code.

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread Stefan Grosse

 Cat=c('a','a','a','b','b','b','a','a','b')# Categorical variable
 C1=vector(length=length(Cat)) # New vector for numeric values

 # Cycle through each column and set C1 to corresponding value of Cat.
 for(i in 1:length(C1)){
   if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
 }

 C1
 [1] -1 -1 -1  1  1  1 -1 -1  1
 Cat
 [1] a a a b b b a a b

   
how about:

Cat-c('a','a','a','b','b','b','a','a','b')
c1- -2*(Cat==a)+1



-=-=-
... Time is an illusion, lunchtime doubly so. (Ford Prefect)

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread Ted Harding
On 04-Jul-07 13:44:44, Keith Alan Chamberlain wrote:
 Dear Rhelpers,
 
 Is there a faster way than below to set a vector based on values
 from another vector? I'd like to call a pre-existing function for
 this, but one which can also handle an arbitrarily large number
 of categories. Any ideas?
 
 Cat=c('a','a','a','b','b','b','a','a','b')# Categorical variable
 C1=vector(length=length(Cat)) # New vector for numeric values
 
# Cycle through each column and set C1 to corresponding value of Cat.
 for(i in 1:length(C1)){
   if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
 }
 
 C1
 [1] -1 -1 -1  1  1  1 -1 -1  1
 Cat
 [1] a a a b b b a a b

 Cat=c('a','a','a','b','b','b','a','a','b')

 Cat==b 
[1] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE

 (Cat==b) - 0.5
[1] -0.5 -0.5 -0.5  0.5  0.5  0.5 -0.5 -0.5  0.5

 2*((Cat==b) - 0.5)
[1] -1 -1 -1  1  1  1 -1 -1  1

to give one example of a way to do it. But you don't say why you
really want to do this. You may really want factors. And what do
you want to see if there is an arbitrarily large number of
categories?

For instance:

 factor(Cat,labels=c(-1,1))
[1] -1 -1 -1 1  1  1  -1 -1 1 

but this is not a vector, but a factor object. To get the vector,
you need to convert Cat to an integer:

 as.integer(factor(Cat))
[1] 1 1 1 2 2 2 1 1 2

where (unless you've specified otherwise in factor()) the values
will correspond to the elements of Cat in natural order, in this
case first a (- 1), then b (- 2).

E.g.

 Cat2-c(a,a,c,b,a,b)
 as.integer(factor(Cat2))
[1] 1 1 3 2 1 2

so, with C2-as.integer(factor(Cat2)), you get a vector of distinct
integers 91,2,3) for the distinct levels (a,b,c) of Cat2.
If you want integer values for these levels, you can write a function
to change them.

Hoping this helps to beark the ice!
Ted.



E-Mail: (Ted Harding) [EMAIL PROTECTED]
Fax-to-email: +44 (0)870 094 0861
Date: 04-Jul-07   Time: 16:44:20
-- XFMail --

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread Gabor Grothendieck
Here are two ways.  The second way is more than 10x faster.

 set.seed(1)
 C - sample(c(a, b), 10, replace = TRUE)
 system.time(s1 - ifelse(C == a, 1, -1))
   user  system elapsed
   0.370.010.38
 system.time(s2 - 2 * (C == a) - 1)
   user  system elapsed
   0.020.000.02
 identical(s1, s2)
[1] TRUE

On 7/4/07, Keith Alan Chamberlain [EMAIL PROTECTED] wrote:
 Dear Rhelpers,

 Is there a faster way than below to set a vector based on values from
 another vector? I'd like to call a pre-existing function for this, but one
 which can also handle an arbitrarily large number of categories. Any ideas?

 Cat=c('a','a','a','b','b','b','a','a','b')  # Categorical variable
 C1=vector(length=length(Cat))   # New vector for numeric values

 # Cycle through each column and set C1 to corresponding value of Cat.
 for(i in 1:length(C1)){
if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
 }

 C1
 [1] -1 -1 -1  1  1  1 -1 -1  1
 Cat
 [1] a a a b b b a a b

 Sincerely,
 KeithC.
 Psych Undergrad, CU Boulder (US)
 RE McNair Scholar

 __
 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
 and provide commented, minimal, self-contained, reproducible code.


__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread joris . dewolf


or
Cat - c('a','a','a','b','b','b','a','a','b')
C1 - (Cat=='a')*1







   
 ONKELINX,
 Thierry  
 Thierry.ONKELINX  To 
 @inbo.be Keith Alan Chamberlain
 Sent by:  [EMAIL PROTECTED],   
 [EMAIL PROTECTED] r-help@stat.math.ethz.ch  
 at.math.ethz.chcc 
   
   Subject 
 04/07/2007 17:17  Re: [R] A More efficient method?
   
   
   
   
   
   




Cat - c('a','a','a','b','b','b','a','a','b')
C1 - ifelse(Cat == 'a', -1, 1)



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
[EMAIL PROTECTED]
www.inbo.be

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney



 -Oorspronkelijk bericht-
 Van: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] Namens Keith Alan
 Chamberlain
 Verzonden: woensdag 4 juli 2007 15:45
 Aan: r-help@stat.math.ethz.ch
 Onderwerp: [R] A More efficient method?

 Dear Rhelpers,

 Is there a faster way than below to set a vector based on
 values from another vector? I'd like to call a pre-existing
 function for this, but one which can also handle an
 arbitrarily large number of categories. Any ideas?

 Cat=c('a','a','a','b','b','b','a','a','b')   # Categorical
variable
 C1=vector(length=length(Cat))# New vector for numeric values

 # Cycle through each column and set C1 to corresponding value of Cat.
 for(i in 1:length(C1)){
if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
 }

 C1
 [1] -1 -1 -1  1  1  1 -1 -1  1
 Cat
 [1] a a a b b b a a b

 Sincerely,
 KeithC.
 Psych Undergrad, CU Boulder (US)
 RE McNair Scholar

 __
 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
 and provide commented, minimal, self-contained, reproducible code.


__
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
and provide commented, minimal, self-contained, reproducible code.

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread Stefan Grosse
Gabor Grothendieck wrote:
 set.seed(1)
 C - sample(c(a, b), 10, replace = TRUE)
 system.time(s1 - ifelse(C == a, 1, -1))
 
user  system elapsed
0.370.010.38
   
 system.time(s2 - 2 * (C == a) - 1)
 
user  system elapsed
0.020.000.02
   
 system.time(s1 - ifelse(C == a, 1, -1))
   user  system elapsed
   0.040.010.08
 system.time(s2 - 2 * (C == a) - 1)
   user  system elapsed
  0   0   0


I am just wondering: how comes the time does add up to 0.05 while
elapsed states 0.08 on my system? (Vista+R2.5.1)

Stefan


-=-=-
... Time is an illusion, lunchtime doubly so. (Ford Prefect)

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread S Ellison
#Given
Cat=c('a','a','a','b','b','b','a','a','b')  # Categorical variable

#and defining 
coding-array(c(-1,1), dimnames=list(unique(Cat) ))

#(ie an array of values corresponding to your character array levels, and with 
names set to those levels)

coding[Cat]

#does what you want.

 Keith Alan Chamberlain [EMAIL PROTECTED] 04/07/2007 14:44:44 
Dear Rhelpers,

Is there a faster way than below to set a vector based on values from
another vector? I'd like to call a pre-existing function for this, but one
which can also handle an arbitrarily large number of categories. Any ideas?

Cat=c('a','a','a','b','b','b','a','a','b')  # Categorical variable
C1=vector(length=length(Cat))   # New vector for numeric values

# Cycle through each column and set C1 to corresponding value of Cat.
for(i in 1:length(C1)){
if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
}

C1
[1] -1 -1 -1  1  1  1 -1 -1  1
Cat
[1] a a a b b b a a b

Sincerely,
KeithC.
Psych Undergrad, CU Boulder (US)
RE McNair Scholar

__
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 
and provide commented, minimal, self-contained, reproducible code.

***
This email and any attachments are confidential. Any use, co...{{dropped}}

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread Keith Alan Chamberlain
Dear Ted,

You are correct in that factors are probably what I had in mind since I
would be using them as predictors in a regression. I didn't know the syntax
to get R to do the arithmetic.

Many thanks to everyone who replied! 

Sincerely,
KeithC.
Psych Undergrad, CU Boulder (US)
RE McNair Scholar

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread Gabor Grothendieck
In thinking about this a bit more I have found a slightly faster one still.
See s3.  Also I have added s0, the original solution, to the timings.

 set.seed(1)
 C - sample(c(a, b), 100, replace = TRUE)
 system.time({
+ s0 - vector(length = length(C))
+ for(i in seq_along(C)) s0[i] - if (C[i] == a) 1 else -1
+ s0
+ })
   user  system elapsed
  21.750.02   25.99
 system.time(s1 - ifelse(C == a, 1, -1))
   user  system elapsed
   2.320.172.54
 system.time(s2 - 2 * (C == a) - 1)
   user  system elapsed
   0.290.020.32
 system.time({tmp - C == a; tmp - !tmp})
   user  system elapsed
   0.210.000.21
 identical(s0, s1)
[1] TRUE
 identical(s0, s2)
[1] TRUE
 identical(s0, s3)
[1] TRUE

On 7/4/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 Here are two ways.  The second way is more than 10x faster.

  set.seed(1)
  C - sample(c(a, b), 10, replace = TRUE)
  system.time(s1 - ifelse(C == a, 1, -1))
   user  system elapsed
   0.370.010.38
  system.time(s2 - 2 * (C == a) - 1)
   user  system elapsed
   0.020.000.02
  identical(s1, s2)
 [1] TRUE

 On 7/4/07, Keith Alan Chamberlain [EMAIL PROTECTED] wrote:
  Dear Rhelpers,
 
  Is there a faster way than below to set a vector based on values from
  another vector? I'd like to call a pre-existing function for this, but one
  which can also handle an arbitrarily large number of categories. Any ideas?
 
  Cat=c('a','a','a','b','b','b','a','a','b')  # Categorical variable
  C1=vector(length=length(Cat))   # New vector for numeric values
 
  # Cycle through each column and set C1 to corresponding value of Cat.
  for(i in 1:length(C1)){
 if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
  }
 
  C1
  [1] -1 -1 -1  1  1  1 -1 -1  1
  Cat
  [1] a a a b b b a a b
 
  Sincerely,
  KeithC.
  Psych Undergrad, CU Boulder (US)
  RE McNair Scholar
 
  __
  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
  and provide commented, minimal, self-contained, reproducible code.
 


__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread Gabor Grothendieck
This was in error since s3 was not set.  The as.numeric in the calculation
of s3 can be omitted if its ok to have an integer rather than numeric result
and in that case its still faster yet.

 set.seed(1)
 C - sample(c(a, b), 100, replace = TRUE)
 system.time({
+ s0 - vector(length = length(C))
+ for(i in seq_along(C)) s0[i] - if (C[i] == a) 1 else -1
+ s0
+ })
   user  system elapsed
  21.320.02   26.10
 system.time(s1 - ifelse(C == a, 1, -1))
   user  system elapsed
   2.370.262.64
 system.time(s2 - 2 * (C == a) - 1)
   user  system elapsed
   0.320.020.35
 system.time({tmp - C == a; s3 - as.numeric(tmp - !tmp)})
   user  system elapsed
   0.280.020.31
 identical(s0, s1)
[1] TRUE
 identical(s0, s2)
[1] TRUE
 identical(s0, s3)
[1] TRUE



On 7/4/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 In thinking about this a bit more I have found a slightly faster one still.
 See s3.  Also I have added s0, the original solution, to the timings.

  set.seed(1)
  C - sample(c(a, b), 100, replace = TRUE)
  system.time({
 + s0 - vector(length = length(C))
 + for(i in seq_along(C)) s0[i] - if (C[i] == a) 1 else -1
 + s0
 + })
   user  system elapsed
  21.750.02   25.99
  system.time(s1 - ifelse(C == a, 1, -1))
   user  system elapsed
   2.320.172.54
  system.time(s2 - 2 * (C == a) - 1)
   user  system elapsed
   0.290.020.32
  system.time({tmp - C == a; tmp - !tmp})
   user  system elapsed
   0.210.000.21
  identical(s0, s1)
 [1] TRUE
  identical(s0, s2)
 [1] TRUE
  identical(s0, s3)
 [1] TRUE

 On 7/4/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
  Here are two ways.  The second way is more than 10x faster.
 
   set.seed(1)
   C - sample(c(a, b), 10, replace = TRUE)
   system.time(s1 - ifelse(C == a, 1, -1))
user  system elapsed
0.370.010.38
   system.time(s2 - 2 * (C == a) - 1)
user  system elapsed
0.020.000.02
   identical(s1, s2)
  [1] TRUE
 
  On 7/4/07, Keith Alan Chamberlain [EMAIL PROTECTED] wrote:
   Dear Rhelpers,
  
   Is there a faster way than below to set a vector based on values from
   another vector? I'd like to call a pre-existing function for this, but one
   which can also handle an arbitrarily large number of categories. Any 
   ideas?
  
   Cat=c('a','a','a','b','b','b','a','a','b')  # Categorical variable
   C1=vector(length=length(Cat))   # New vector for numeric values
  
   # Cycle through each column and set C1 to corresponding value of Cat.
   for(i in 1:length(C1)){
  if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
   }
  
   C1
   [1] -1 -1 -1  1  1  1 -1 -1  1
   Cat
   [1] a a a b b b a a b
  
   Sincerely,
   KeithC.
   Psych Undergrad, CU Boulder (US)
   RE McNair Scholar
  
   __
   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
   and provide commented, minimal, self-contained, reproducible code.
  
 


__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread François Pinard
[Keith Alan Chamberlain]

Is there a faster way than below to set a vector based on values
from another vector? I'd like to call a pre-existing function for
this, but one which can also handle an arbitrarily large number of
categories. Any ideas?

Cat=c('a','a','a','b','b','b','a','a','b') # Categorical variable
C1=vector(length=length(Cat))  # New vector for numeric values

# Cycle through each column and set C1 to corresponding value of Cat.
for(i in 1:length(C1)){
   if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
}

C1
[1] -1 -1 -1  1  1  1 -1 -1  1
Cat
[1] a a a b b b a a b

For handling an arbitrarily large number of categories, one may go
through a recoding vector, like this for the example above:

 Cat - c('a', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'b')
 C1 - c(a=-1, b=1)[Cat]
 C1
 a  a  a  b  b  b  a  a  b
-1 -1 -1  1  1  1 -1 -1  1

-- 
François Pinard   http://pinard.progiciels-bpi.ca

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread jim holtman
User and System are a measure of the CPU time that was consumed.  Elapsed
time is the wall clock and even though they are both measured in seconds,
they are not really the same units.  The reason for the difference is any
idle time that they system may have waiting for I/O to complete which does
not consume CPU time for your process, but does consume Elasped time.

For some instances of CPU intensive code (with no I/O of competing tasks),
the User + System ~= Elapsed.  Also you have to take into account the
granularity of the clock when looking at numbers like 0.04.  So serious
comparisons of timing, you want runs of at least 10s of seconds or more.


On 7/4/07, Stefan Grosse [EMAIL PROTECTED] wrote:

 Gabor Grothendieck wrote:
  set.seed(1)
  C - sample(c(a, b), 10, replace = TRUE)
  system.time(s1 - ifelse(C == a, 1, -1))
 
 user  system elapsed
 0.370.010.38
 
  system.time(s2 - 2 * (C == a) - 1)
 
 user  system elapsed
 0.020.000.02
 
  system.time(s1 - ifelse(C == a, 1, -1))
   user  system elapsed
   0.040.010.08
  system.time(s2 - 2 * (C == a) - 1)
   user  system elapsed
  0   0   0


 I am just wondering: how comes the time does add up to 0.05 while
 elapsed states 0.08 on my system? (Vista+R2.5.1)

 Stefan


 -=-=-
 ... Time is an illusion, lunchtime doubly so. (Ford Prefect)

 __
 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
 and provide commented, minimal, self-contained, reproducible code.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

[[alternative HTML version deleted]]

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] A More efficient method?

2007-07-04 Thread jim holtman
One other thing, in a multiprocessor configuration, if your application is
making use of the additional CPUs, then

User + System  Elapsed

In some cases.


On 7/4/07, jim holtman [EMAIL PROTECTED] wrote:

 User and System are a measure of the CPU time that was consumed.  Elapsed
 time is the wall clock and even though they are both measured in seconds,
 they are not really the same units.  The reason for the difference is any
 idle time that they system may have waiting for I/O to complete which does
 not consume CPU time for your process, but does consume Elasped time.

 For some instances of CPU intensive code (with no I/O of competing tasks),
 the User + System ~= Elapsed.  Also you have to take into account the
 granularity of the clock when looking at numbers like 0.04.  So serious
 comparisons of timing, you want runs of at least 10s of seconds or more.


  On 7/4/07, Stefan Grosse [EMAIL PROTECTED] wrote:
 
  Gabor Grothendieck wrote:
   set.seed(1)
   C - sample(c(a, b), 10, replace = TRUE)
   system.time(s1 - ifelse(C == a, 1, -1))
  
  user  system elapsed
  0.370.010.38
  
   system.time(s2 - 2 * (C == a) - 1)
  
  user  system elapsed
  0.020.000.02
  
   system.time(s1 - ifelse(C == a, 1, -1))
user  system elapsed
0.040.010.08
   system.time(s2 - 2 * (C == a) - 1)
user  system elapsed
   0   0   0
 
 
  I am just wondering: how comes the time does add up to 0.05 while
  elapsed states 0.08 on my system? (Vista+R2.5.1)
 
  Stefan
 
 
  -=-=-
  ... Time is an illusion, lunchtime doubly so. (Ford Prefect)
 
  __
  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.htmlhttp://www.r-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.
 



 --
 Jim Holtman
 Cincinnati, OH
 +1 513 646 9390

 What is the problem you are trying to solve?




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

[[alternative HTML version deleted]]

__
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
and provide commented, minimal, self-contained, reproducible code.