Re: [R] how to get all iterations if I meet NaN?

2009-03-29 Thread Nash
 Uwe Ligges wrote:
 Please read the question more carefully, the sin() example was used 
 as a method that does not give an error but works as expected (just 
 with the warning), but the question is how not to break the loop,
  and so my answer was see ?try.

So,
Do you have any solution about his problem ?

  On Sat, 28 Mar 2009 01:36:36 +0800, huiming song wrote
  hi, everybody, please help me with this question:
 
  If I want to do iteration for 1000 times, however, for the 500th 
  iteration, there is NaN appears. Then the iteration will stop. If I 
  don't want the stop and want the all the 1000 iterations be done. 
  What shall I do?
 
  suppose I have x[1:1000] and z[1:1000],I want to do some calculation 
  for all x[1] to x[1000].
 
  z=rep(0,1000)
  for (i in 1:1000){
z[i]=sin(1/x[i])
  }
 
  if x[900] is 0, in the above code it will not stop when NaN appears. 
  Suppose when sin(1/x[900]) is NaN appears and the iteration will now 
  fulfill the rest 100 iterations. How can I write a code to let all 
  the 1000 iterations be done?
 
  Thanks!




On Sat, 28 Mar 2009 16:35:24 +0100, Uwe Ligges wrote
 Nash wrote:
  hi, you can try this method.
  
  ## if x is a vector
  x - runif(1000)
  ## if sin(1/0) will appear NaN. we make the situation. 
  x[sample(1:length(x),5)] - 0   
  z - suppressWarnings(ifelse(is.na(sin(1/x)),NA,sin(1/x)))
  is.numeric(z)
  
  ## if x is a matrix
  x=matrix(runif(1000),100,10)
  x[sample(1:nrow(x),50),sample(1:ncol(x),5)] - 0
  z - suppressWarnings(ifelse(is.na(sin(1/x)),NA,sin(1/x)))
  is.numeric(z)
 
 
 Please read the question more carefully, the sin() example was used 
 as a method that does not give an error but works as expected (just 
 with the warning), but the question is how not to break the loop,
  and so my answer was see ?try.
 
 Uwe Ligges
 
  
  On Sat, 28 Mar 2009 01:36:36 +0800, huiming song wrote
  hi, everybody, please help me with this question:
 
  If I want to do iteration for 1000 times, however, for the 500th 
  iteration, there is NaN appears. Then the iteration will stop. If I 
  don't want the stop and want the all the 1000 iterations be done. 
  What shall I do?
 
  suppose I have x[1:1000] and z[1:1000],I want to do some calculation 
  for all x[1] to x[1000].
 
  z=rep(0,1000)
  for (i in 1:1000){
z[i]=sin(1/x[i])
  }
 
  if x[900] is 0, in the above code it will not stop when NaN appears. 
  Suppose when sin(1/x[900]) is NaN appears and the iteration will now 
  fulfill the rest 100 iterations. How can I write a code to let all 
  the 1000 iterations be done?
 
  Thanks!
 
 [[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.
  
  
  --
  Nash - morri...@ibms.sinica.edu.tw
  
  __
  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.


--
Nash - morri...@ibms.sinica.edu.tw

__
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 get all iterations if I meet NaN?

2009-03-29 Thread Peter Dalgaard

Nash wrote:

Uwe Ligges wrote:
Please read the question more carefully, the sin() example was used 
as a method that does not give an error but works as expected (just 
with the warning), but the question is how not to break the loop,

 and so my answer was see ?try.


So,
Do you have any solution about his problem ?



Yes he does: Use try().

This may not be very specific, but neither was the original question. 
The poster didn't give an example that we could fix, only the sin() 
example that he wanted his own code to work _like_.


So to answer the question with working code, one first needs to invent a 
broken example. Perhaps something like


for(i in 2:-2) {
   x - try(chol(as.matrix(i)))
   if(inherits(x,try-error)) x - NaN
   print(x)
}


--
   O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph:  (+45) 35327918
~~ - (p.dalga...@biostat.ku.dk)  FAX: (+45) 35327907

__
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 get all iterations if I meet NaN?

2009-03-29 Thread Nash
Okay!

Thank you!

On Sun, 29 Mar 2009 10:01:29 +0200, Peter Dalgaard wrote
 Nash wrote:
  Uwe Ligges wrote:
  Please read the question more carefully, the sin() example was used 
  as a method that does not give an error but works as expected (just 
  with the warning), but the question is how not to break the loop,
   and so my answer was see ?try.
  
  So,
  Do you have any solution about his problem ?
 
 
 Yes he does: Use try().
 
 This may not be very specific, but neither was the original 
 question. The poster didn't give an example that we could fix, only 
 the sin() example that he wanted his own code to work _like_.
 
 So to answer the question with working code, one first needs to 
 invent a broken example. Perhaps something like
 
 for(i in 2:-2) {
 x - try(chol(as.matrix(i)))
 if(inherits(x,try-error)) x - NaN
 print(x)
 }
 
 -- 
 O__   Peter Dalgaard 喒ter Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 
   (*) \(*) -- University of Copenhagen   Denmark  Ph:  (+45) 35327918
 ~~ - (p.dalga...@biostat.ku.dk)  FAX: (+45) 35327907


--
Nash - morri...@ibms.sinica.edu.tw

__
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 get all iterations if I meet NaN?

2009-03-28 Thread Nash
hi, you can try this method.

## if x is a vector
x - runif(1000)
## if sin(1/0) will appear NaN. we make the situation. 
x[sample(1:length(x),5)] - 0   
z - suppressWarnings(ifelse(is.na(sin(1/x)),NA,sin(1/x)))
is.numeric(z)

## if x is a matrix
x=matrix(runif(1000),100,10)
x[sample(1:nrow(x),50),sample(1:ncol(x),5)] - 0
z - suppressWarnings(ifelse(is.na(sin(1/x)),NA,sin(1/x)))
is.numeric(z)



On Sat, 28 Mar 2009 01:36:36 +0800, huiming song wrote
 hi, everybody, please help me with this question:
 
 If I want to do iteration for 1000 times, however, for the 500th 
 iteration, there is NaN appears. Then the iteration will stop. If I 
 don't want the stop and want the all the 1000 iterations be done. 
 What shall I do?
 
 suppose I have x[1:1000] and z[1:1000],I want to do some calculation 
 for all x[1] to x[1000].
 
 z=rep(0,1000)
 for (i in 1:1000){
   z[i]=sin(1/x[i])
 }
 
 if x[900] is 0, in the above code it will not stop when NaN appears. 
 Suppose when sin(1/x[900]) is NaN appears and the iteration will now 
 fulfill the rest 100 iterations. How can I write a code to let all 
 the 1000 iterations be done?
 
 Thanks!
 
   [[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.


--
Nash - morri...@ibms.sinica.edu.tw

__
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 get all iterations if I meet NaN?

2009-03-28 Thread Uwe Ligges



Nash wrote:

hi, you can try this method.

## if x is a vector
x - runif(1000)
## if sin(1/0) will appear NaN. we make the situation. 
x[sample(1:length(x),5)] - 0   
z - suppressWarnings(ifelse(is.na(sin(1/x)),NA,sin(1/x)))

is.numeric(z)

## if x is a matrix
x=matrix(runif(1000),100,10)
x[sample(1:nrow(x),50),sample(1:ncol(x),5)] - 0
z - suppressWarnings(ifelse(is.na(sin(1/x)),NA,sin(1/x)))
is.numeric(z)




Please read the question more carefully, the sin() example was used as a 
method that does not give an error but works as expected (just with the 
warning), but the question is how not to break the loop, and so my 
answer was see ?try.


Uwe Ligges



On Sat, 28 Mar 2009 01:36:36 +0800, huiming song wrote

hi, everybody, please help me with this question:

If I want to do iteration for 1000 times, however, for the 500th 
iteration, there is NaN appears. Then the iteration will stop. If I 
don't want the stop and want the all the 1000 iterations be done. 
What shall I do?


suppose I have x[1:1000] and z[1:1000],I want to do some calculation 
for all x[1] to x[1000].


z=rep(0,1000)
for (i in 1:1000){
  z[i]=sin(1/x[i])
}

if x[900] is 0, in the above code it will not stop when NaN appears. 
Suppose when sin(1/x[900]) is NaN appears and the iteration will now 
fulfill the rest 100 iterations. How can I write a code to let all 
the 1000 iterations be done?


Thanks!

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



--
Nash - morri...@ibms.sinica.edu.tw

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] how to get all iterations if I meet NaN?

2009-03-27 Thread huiming song
hi, everybody, please help me with this question:

If I want to do iteration for 1000 times, however, for the 500th iteration,
there is NaN appears. Then the iteration will stop. If I don't want the stop
and want the all the 1000 iterations be done. What shall I do?


suppose I have x[1:1000] and z[1:1000],I want to do some calculation for all
x[1] to x[1000].

z=rep(0,1000)
for (i in 1:1000){
  z[i]=sin(1/x[i])
}

if x[900] is 0, in the above code it will not stop when NaN appears. Suppose
when sin(1/x[900]) is NaN appears and the iteration will now fulfill the
rest 100 iterations. How can I write a code to let all the 1000 iterations
be done?

Thanks!

[[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] how to get all iterations if I meet NaN?

2009-03-27 Thread Uwe Ligges



huiming song wrote:

hi, everybody, please help me with this question:

If I want to do iteration for 1000 times, however, for the 500th iteration,
there is NaN appears. Then the iteration will stop. If I don't want the stop
and want the all the 1000 iterations be done. What shall I do?


suppose I have x[1:1000] and z[1:1000],I want to do some calculation for all
x[1] to x[1000].

z=rep(0,1000)
for (i in 1:1000){
  z[i]=sin(1/x[i])
}

if x[900] is 0, in the above code it will not stop when NaN appears. Suppose
when sin(1/x[900]) is NaN appears and the iteration will now fulfill the
rest 100 iterations. How can I write a code to let all the 1000 iterations
be done?



See ?try

Uwe Ligges



Thanks!

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to get all iterations if I meet NaN?

2009-03-27 Thread Patrizio Frederic
2009/3/27 huiming song huimi...@gmail.com:
 hi, everybody, please help me with this question:

 If I want to do iteration for 1000 times, however, for the 500th iteration,
 there is NaN appears. Then the iteration will stop. If I don't want the stop
 and want the all the 1000 iterations be done. What shall I do?


 suppose I have x[1:1000] and z[1:1000],I want to do some calculation for all
 x[1] to x[1000].

 z=rep(0,1000)
 for (i in 1:1000){
  z[i]=sin(1/x[i])
 }

 if x[900] is 0, in the above code it will not stop when NaN appears. Suppose
 when sin(1/x[900]) is NaN appears and the iteration will now fulfill the
 rest 100 iterations. How can I write a code to let all the 1000 iterations
 be done?


not sure I properly understood. Consider:

 x = seq(-pi,pi,length=1001)
 z = sin(1/x)
# Warning message:
# In sin(1/x) : NaNs produced
x[500:502]; z[500:502]
# [1] -0.006283185  0.0  0.006283185
# [1] -0.8754095NaN  0.8754095

one NaN and one warning have been created, the remaining 1000
calculations has been executed.

lim(x-0)sin(1/x) not exists so sin(1/0) is not a number nan

z1 = z[!is.nan(z)]
x1 = x[!is.nan(z)]

# x and z without the z's nan position

hope that help

Patrizio

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