Re: [R] different functions on different vector subsets

2005-11-11 Thread Ron Ophir
I thought about other cases but I have to dissagree with you. For
logical vector NA is no decision and that should be the results of it.
Let's say that -b- is a result of comparison not of -a- to something 
rather a compsrison of -c- to -d-. In this case NA in the first position
is a result of NA in either -c- or -d- or both. Now, if the result I
wanted to get from your example

a-c(1,2,3,4)
b-c(NA,T,F,T)

a[b]-7
 is c(1,7,7,7) I should replace the NA (no decision) with F and if to
get c(7,7,7,7)  the NA should be replaced by T otherwise the result
should be c(NA,7,7,7). The first two option are possible to perform in R
 the third is not and that is to the user to decide which to choose.
Ron

 Thomas Lumley [EMAIL PROTECTED] 11/10/05 11:02 PM 
On Thu, 10 Nov 2005, Ron Ophir wrote:

 Thanks Thomas,

 ...For logical subscripts you could argue that the
 ambiguity isn't present and that if the index was NA the element
should
 just be set to NA. This change might be worth making.

 I see you got my point. NA should return NA no matter what the
 comparison is.

I'm not sure that I did get your point.  As Brian said, you aren't 
specifying whether or not to set the value. In your example it didn't 
matter because it would end up NA either way.

I was saying that for eg

a-c(1,2,3,4)
b-c(NA,T,F,T)

a[b]-7

we could relax the prohibition on NA indexing to give c(NA,7,7,7) as the

result. In your case that would give what you wanted, but in other cases

it might not.


-thomas

__
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] different functions on different vector subsets

2005-11-11 Thread Thomas Lumley
On Fri, 11 Nov 2005, Ron Ophir wrote:

 I thought about other cases but I have to dissagree with you. For
 logical vector NA is no decision and that should be the results of it.

I would say NA is missing: it means that the result could be any valid 
value and we don't know which one it is.  That's how NA works in all other 
computations. It doesn't really affect your argument below, though.

 Let's say that -b- is a result of comparison not of -a- to something
 rather a compsrison of -c- to -d-. In this case NA in the first position
 is a result of NA in either -c- or -d- or both. Now, if the result I
 wanted to get from your example

 a-c(1,2,3,4)
 b-c(NA,T,F,T)

 a[b]-7
 is c(1,7,7,7) I should replace the NA (no decision) with F and if to
 get c(7,7,7,7)  the NA should be replaced by T otherwise the result
 should be c(NA,7,7,7). The first two option are possible to perform in R
 the third is not and that is to the user to decide which to choose.

But as Brian pointed out, what if a is a list? There is no NA value for 
the list type.  So assignment with an NA subscript cannot be made 
consistent across even the basic vector types.

-thomas


 Ron

 Thomas Lumley [EMAIL PROTECTED] 11/10/05 11:02 PM 
 On Thu, 10 Nov 2005, Ron Ophir wrote:

 Thanks Thomas,

 ...For logical subscripts you could argue that the
 ambiguity isn't present and that if the index was NA the element
 should
 just be set to NA. This change might be worth making.

 I see you got my point. NA should return NA no matter what the
 comparison is.

 I'm not sure that I did get your point.  As Brian said, you aren't
 specifying whether or not to set the value. In your example it didn't
 matter because it would end up NA either way.

 I was saying that for eg

 a-c(1,2,3,4)
 b-c(NA,T,F,T)

 a[b]-7

 we could relax the prohibition on NA indexing to give c(NA,7,7,7) as the

 result. In your case that would give what you wanted, but in other cases

 it might not.


   -thomas

 __
 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


Thomas Lumley   Assoc. Professor, Biostatistics
[EMAIL PROTECTED]   University of Washington, Seattle

__
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] different functions on different vector subsets

2005-11-10 Thread Ron Ophir
Hi,
I am trying to apply two different functions on on a vector as follow:
a-c(NA,1,2,3,-3,-4,-6)
if a0 I would like to raise it by the power of 2: 2^a and if the a0 I
would like to have the inverse value, i.e., -1/2^a.
so I thought of doing it two steps:
a[a0]-2^[a0]
a[a0]-(-1)/2^a[a0]
I got the following error
Error: NAs are not allowed in subscripted assignments
any other manupulation that I did with is.na() but did not succeed.
What is funny that the two sides of the assignment work and return the
same vector size:
 2^a[a0]
[1] NA  2  4  8
 a[a0]
[1] NA  1  2  3

I found a solution in term of:
sapply(a,function(x) if (is(s.na)) NA else if (x0) (-1)/2^x else 2^x)
but still I would like to understand why the solution above did not
work. I think is more ellegant.
my R version is:
 sessionInfo()
R version 2.2.0, 2005-10-06, i386-pc-mingw32 
 
attached base packages:
[1] methods   stats graphics  grDevices utils
datasets 
[7] base 
Thanks,
Ron
 

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


Re: [R] different functions on different vector subsets

2005-11-10 Thread Berton Gunter
The error messages mean what they say.

 I am trying to apply two different functions on on a vector as follow:
 a-c(NA,1,2,3,-3,-4,-6)
 if a0 I would like to raise it by the power of 2: 2^a and if 
 the a0 I
 would like to have the inverse value, i.e., -1/2^a.
## I assume you mean 1/(2^a). If not, modify the following appropriately.

2^(a*sign(a))  ## will do

As for your error message for:
 a[a0]-(-1)/2^a[a0]

a0 has an NA at the first index and so R doesn't know what index you want
to assign the value to. Ergo the error message.

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
The business of the statistician is to catalyze the scientific learning
process.  - George E. P. Box
 
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of Ron Ophir
 Sent: Thursday, November 10, 2005 7:26 AM
 To: r-help@stat.math.ethz.ch
 Subject: [R] different functions on different vector subsets
 
 Hi,
 I am trying to apply two different functions on on a vector as follow:
 a-c(NA,1,2,3,-3,-4,-6)
 if a0 I would like to raise it by the power of 2: 2^a and if 
 the a0 I
 would like to have the inverse value, i.e., -1/2^a.
 so I thought of doing it two steps:
 a[a0]-2^[a0]
 a[a0]-(-1)/2^a[a0]
 I got the following error
 Error: NAs are not allowed in subscripted assignments
 any other manupulation that I did with is.na() but did not succeed.
 What is funny that the two sides of the assignment work and return the
 same vector size:
  2^a[a0]
 [1] NA  2  4  8
  a[a0]
 [1] NA  1  2  3
 
 I found a solution in term of:
 sapply(a,function(x) if (is(s.na)) NA else if (x0) (-1)/2^x else 2^x)
 but still I would like to understand why the solution above did not
 work. I think is more ellegant.
 my R version is:
  sessionInfo()
 R version 2.2.0, 2005-10-06, i386-pc-mingw32 
  
 attached base packages:
 [1] methods   stats graphics  grDevices utils
 datasets 
 [7] base 
 Thanks,
 Ron
  
 
   [[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


__
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] different functions on different vector subsets

2005-11-10 Thread Thomas Lumley
On Thu, 10 Nov 2005, Ron Ophir wrote:

 Hi,
 I am trying to apply two different functions on on a vector as follow:
 a-c(NA,1,2,3,-3,-4,-6)
 if a0 I would like to raise it by the power of 2: 2^a and if the a0 I
 would like to have the inverse value, i.e., -1/2^a.
 so I thought of doing it two steps:
 a[a0]-2^[a0]
 a[a0]-(-1)/2^a[a0]
 I got the following error
 Error: NAs are not allowed in subscripted assignments
 any other manupulation that I did with is.na() but did not succeed.
 What is funny that the two sides of the assignment work and return the
 same vector size:
 2^a[a0]
 [1] NA  2  4  8
 a[a0]
 [1] NA  1  2  3

The reason NAs are not allowed in subscripted assignments is based on 
numeric rather than logical subscripts.

For numeric subscripts the problem is ambiguity about what the NA index 
should do (we know there is ambiguity because two parts of the R code did 
different things).  For logical subscripts you could argue that the 
ambiguity isn't present and that if the index was NA the element should 
just be set to NA. This change might be worth making.


 I found a solution in term of:
 sapply(a,function(x) if (is(s.na)) NA else if (x0) (-1)/2^x else 2^x)
 but still I would like to understand why the solution above did not
 work. I think is more ellegant.

A better general solution is

  a-ifelse(a0, -1/2^a, 2^a)

An alternative for this problem that is faster when a is very large is
  a-sign(a)*2^abs(a)

-thomas

__
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] different functions on different vector subsets

2005-11-10 Thread Berton Gunter

Oops. Sorry. Should be:

sign(a)*2^a

where I assume you meant the inverse value should be -1/2^|a| = - 2^a for
a0

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
The business of the statistician is to catalyze the scientific learning
process.  - George E. P. Box
 
 

 -Original Message-
 From: bgunter 
 Sent: Thursday, November 10, 2005 9:00 AM
 To: Ron Ophir; r-help@stat.math.ethz.ch
 Subject: RE: [R] different functions on different vector subsets
 
 The error messages mean what they say.
 
  I am trying to apply two different functions on on a vector 
 as follow:
  a-c(NA,1,2,3,-3,-4,-6)
  if a0 I would like to raise it by the power of 2: 2^a and if 
  the a0 I
  would like to have the inverse value, i.e., -1/2^a.
 ## I assume you mean 1/(2^a). If not, modify the following 
 appropriately.
 
 2^(a*sign(a))  ## will do
 
 As for your error message for:
  a[a0]-(-1)/2^a[a0]
 
 a0 has an NA at the first index and so R doesn't know what 
 index you want to assign the value to. Ergo the error message.
 
 -- Bert Gunter
 Genentech Non-Clinical Statistics
 South San Francisco, CA
  
 The business of the statistician is to catalyze the 
 scientific learning process.  - George E. P. Box
  
  
 
  -Original Message-
  From: [EMAIL PROTECTED] 
  [mailto:[EMAIL PROTECTED] On Behalf Of Ron Ophir
  Sent: Thursday, November 10, 2005 7:26 AM
  To: r-help@stat.math.ethz.ch
  Subject: [R] different functions on different vector subsets
  
  Hi,
  I am trying to apply two different functions on on a vector 
 as follow:
  a-c(NA,1,2,3,-3,-4,-6)
  if a0 I would like to raise it by the power of 2: 2^a and if 
  the a0 I
  would like to have the inverse value, i.e., -1/2^a.
  so I thought of doing it two steps:
  a[a0]-2^[a0]
  a[a0]-(-1)/2^a[a0]
  I got the following error
  Error: NAs are not allowed in subscripted assignments
  any other manupulation that I did with is.na() but did not succeed.
  What is funny that the two sides of the assignment work and 
 return the
  same vector size:
   2^a[a0]
  [1] NA  2  4  8
   a[a0]
  [1] NA  1  2  3
  
  I found a solution in term of:
  sapply(a,function(x) if (is(s.na)) NA else if (x0) 
 (-1)/2^x else 2^x)
  but still I would like to understand why the solution above did not
  work. I think is more ellegant.
  my R version is:
   sessionInfo()
  R version 2.2.0, 2005-10-06, i386-pc-mingw32 
   
  attached base packages:
  [1] methods   stats graphics  grDevices utils
  datasets 
  [7] base 
  Thanks,
  Ron
   
  
  [[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
  


__
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] different functions on different vector subsets

2005-11-10 Thread Prof Brian Ripley
On Thu, 10 Nov 2005, Ron Ophir wrote:

 Hi,
 I am trying to apply two different functions on on a vector as follow:
 a-c(NA,1,2,3,-3,-4,-6)
 if a0 I would like to raise it by the power of 2: 2^a and if the a0 I
 would like to have the inverse value, i.e., -1/2^a.
 so I thought of doing it two steps:
 a[a0]-2^[a0]
 a[a0]-(-1)/2^a[a0]
 I got the following error
 Error: NAs are not allowed in subscripted assignments
 any other manupulation that I did with is.na() but did not succeed.
 What is funny that the two sides of the assignment work and return the
 same vector size:
 2^a[a0]
 [1] NA  2  4  8
 a[a0]
 [1] NA  1  2  3

 I found a solution in term of:
 sapply(a,function(x) if (is(s.na)) NA else if (x0) (-1)/2^x else 2^x)
 but still I would like to understand why the solution above did not
 work. I think is more ellegant.

What do you think the NA value in

 a  0
[1]NA  TRUE  TRUE  TRUE FALSE FALSE FALSE

means?  Should you replace a[1] or not?  You are saying you don't know, so 
what is R to do?  It tells you to make up your mind.

Try

ind - !is.na(a)  a  0
a[ind] - 2^a[ind]
ind - !is.na(a)  a  0
a[ind] - (-1)/2^a[ind]

or use ifelse as in

ifelse(a  0, 2^a, -1/2^a)

which is a lot more elegant.

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
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] different functions on different vector subsets

2005-11-10 Thread Ron Ophir
Thanks Thomas,

...For logical subscripts you could argue that the 
ambiguity isn't present and that if the index was NA the element should 
just be set to NA. This change might be worth making.

I see you got my point. NA should return NA no matter what the
comparison is. But any way thanks Brian, Jim, and Berton, I have leaned
a lot. It was a good practice.
Ron

Ron Ophir, Ph.D.
Bioinformatician,
Biological Services
Weizmann Institute of Science
POB 26
Rehovot 76100
Israel
e-mail: [EMAIL PROTECTED]
Phone: 972-8-9342614
Fax:972-8-9344113
 Thomas Lumley [EMAIL PROTECTED] 11/10/05 7:04 PM 
On Thu, 10 Nov 2005, Ron Ophir wrote:

 Hi,
 I am trying to apply two different functions on on a vector as follow:
 a-c(NA,1,2,3,-3,-4,-6)
 if a0 I would like to raise it by the power of 2: 2^a and if the a0
I
 would like to have the inverse value, i.e., -1/2^a.
 so I thought of doing it two steps:
 a[a0]-2^[a0]
 a[a0]-(-1)/2^a[a0]
 I got the following error
 Error: NAs are not allowed in subscripted assignments
 any other manupulation that I did with is.na() but did not succeed.
 What is funny that the two sides of the assignment work and return the
 same vector size:
 2^a[a0]
 [1] NA  2  4  8
 a[a0]
 [1] NA  1  2  3

The reason NAs are not allowed in subscripted assignments is based on 
numeric rather than logical subscripts.

For numeric subscripts the problem is ambiguity about what the NA index 
should do (we know there is ambiguity because two parts of the R code
did 
different things).  For logical subscripts you could argue that the 
ambiguity isn't present and that if the index was NA the element should 
just be set to NA. This change might be worth making.


 I found a solution in term of:
 sapply(a,function(x) if (is(s.na)) NA else if (x0) (-1)/2^x else 2^x)
 but still I would like to understand why the solution above did not
 work. I think is more ellegant.

A better general solution is

  a-ifelse(a0, -1/2^a, 2^a)

An alternative for this problem that is faster when a is very large is
  a-sign(a)*2^abs(a)

-thomas

__
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] different functions on different vector subsets

2005-11-10 Thread Prof Brian Ripley
On Thu, 10 Nov 2005, Thomas Lumley wrote:

 On Thu, 10 Nov 2005, Ron Ophir wrote:

 Hi,
 I am trying to apply two different functions on on a vector as follow:
 a-c(NA,1,2,3,-3,-4,-6)
 if a0 I would like to raise it by the power of 2: 2^a and if the a0 I
 would like to have the inverse value, i.e., -1/2^a.
 so I thought of doing it two steps:
 a[a0]-2^[a0]
 a[a0]-(-1)/2^a[a0]
 I got the following error
 Error: NAs are not allowed in subscripted assignments
 any other manupulation that I did with is.na() but did not succeed.
 What is funny that the two sides of the assignment work and return the
 same vector size:
 2^a[a0]
 [1] NA  2  4  8
 a[a0]
 [1] NA  1  2  3

 The reason NAs are not allowed in subscripted assignments is based on
 numeric rather than logical subscripts.

 For numeric subscripts the problem is ambiguity about what the NA index
 should do (we know there is ambiguity because two parts of the R code did
 different things).  For logical subscripts you could argue that the
 ambiguity isn't present and that if the index was NA the element should
 just be set to NA. This change might be worth making.

That presumes NA is a valid value, but in general it is not. (Not for raw, 
not for lists, not for data frames, )  I don't think we want such 
inconsistent behaviour.

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
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] different functions on different vector subsets

2005-11-10 Thread Thomas Lumley
On Thu, 10 Nov 2005, Ron Ophir wrote:

 Thanks Thomas,

 ...For logical subscripts you could argue that the
 ambiguity isn't present and that if the index was NA the element should
 just be set to NA. This change might be worth making.

 I see you got my point. NA should return NA no matter what the
 comparison is.

I'm not sure that I did get your point.  As Brian said, you aren't 
specifying whether or not to set the value. In your example it didn't 
matter because it would end up NA either way.

I was saying that for eg

a-c(1,2,3,4)
b-c(NA,T,F,T)

a[b]-7

we could relax the prohibition on NA indexing to give c(NA,7,7,7) as the 
result. In your case that would give what you wanted, but in other cases 
it might not.


-thomas

__
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] different functions on different vector subsets

2005-11-10 Thread Thomas Lumley
On Thu, 10 Nov 2005, Thomas Lumley wrote:

 On Thu, 10 Nov 2005, Ron Ophir wrote:

 Thanks Thomas,
 
 ...For logical subscripts you could argue that the
 ambiguity isn't present and that if the index was NA the element should
 just be set to NA. This change might be worth making.
 
 I see you got my point. NA should return NA no matter what the
 comparison is.

 I'm not sure that I did get your point.  As Brian said, you aren't specifying 
 whether or not to set the value. In your example it didn't matter because it 
 would end up NA either way.


And as Brian later pointed out, this approach wouldn't work for things 
other than simple vectors.

-thomas

__
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