Re: [R] recovering from errors with lapply()

2013-02-13 Thread Andrew Barr
Thanks very much Milan.  Your answer makes perfect sense.

Best,

Andrew


On Wed, Feb 13, 2013 at 3:51 AM, Milan Bouchet-Valat nalimi...@club.frwrote:

 Le mardi 12 février 2013 à 15:41 -0600, Andrew Barr a écrit :
  Hi all,
 
  I am searching for a way to recover results from lapply() when one or
 more
  values returns an error.  I have a written a function that uses
 tryCatch()
  to recover from errors. Here is a very simple example of such a function.
 
  divideBy2-function(X){
result-tryCatch(X/2, error = function(e) An Error Occurred)
return(result)
  }
 
  This function appears to work as I expect both in cases where an error
  occurs and where no error occurs.
 
  divideBy2(10)
  # [1] 5
 
  divideBy2(This is not a number)
  # [1] An Error Occurred
 
  I would like to use this function in an lapply(), but when I do so ALL of
  the results returned are the results of an error.
 
  lapply(c(10,This is not a number),FUN=divideBy2)
 
  #[[1]]
  #[1] An Error Occurred
 
  #[[2]]
  #[1] An Error Occurred
 
  Is this how lapply() is meant to work? What I want is a list that looks
  like this
 The problem happens before lapply() itself:
  c(10,This is not a number)
 [1] 10  This is not a number

 A vector can only contain values of the same type, so 10 is converted to
 a character value, and divideBy2(10) does not work. Try with:
 lapply(list(10, This is not a number), divideBy2)

 All that means that in real use cases, your tryCatch() solution should
 work (I have not tested it). The bug is in your toy example.


 Regards

  #[[1]]
  #[1] 5
  #[[2]]
  #[1] An Error Occurred
 
  Thanks very much for your time.
 
[[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.



[[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] recovering from errors with lapply()

2013-02-12 Thread Andrew Barr
Hi all,

I am searching for a way to recover results from lapply() when one or more
values returns an error.  I have a written a function that uses tryCatch()
to recover from errors. Here is a very simple example of such a function.

divideBy2-function(X){
  result-tryCatch(X/2, error = function(e) An Error Occurred)
  return(result)
}

This function appears to work as I expect both in cases where an error
occurs and where no error occurs.

divideBy2(10)
# [1] 5

divideBy2(This is not a number)
# [1] An Error Occurred

I would like to use this function in an lapply(), but when I do so ALL of
the results returned are the results of an error.

lapply(c(10,This is not a number),FUN=divideBy2)

#[[1]]
#[1] An Error Occurred

#[[2]]
#[1] An Error Occurred

Is this how lapply() is meant to work? What I want is a list that looks
like this

#[[1]]
#[1] 5
#[[2]]
#[1] An Error Occurred

Thanks very much for your time.

[[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] Assigning value to a vector from within a function

2010-10-06 Thread Andrew Barr
Hi all,

I am having trouble assigning a value within a vector using a variable
defined within a function.  I suspect this has something to do with
namespaces but I cannot figure it out.  Please reply directly to me if you
can help.

###begin code
#simple vector
test-c(4,5,6)

#simple function to get a value from a vector
#works just like I would expect, has no problem indexing using the local
function variable name index

getvalue-function(index){
print(test[index])
}
getvalue(2)


#another simple function to update the value in a vector
update-function(index){
test[index]- 20
}
update(2)
test
#The update() function silently fails to accomplish the update
###

I don't understand why the function has no problem accessing the value, but
cannot accomplish the assignment.  I need to be able to update vectors using
index values which I pass from within a function.

Thanks for your help.

Andrew Barr

[[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 me avoid nested for() loops!

2009-11-20 Thread Andrew Barr
Hi R folks,

I have a massive array (object name points) in the following form

 [,1] [,2]
[1,] 1369   22
[2,] 1370   22
[3,] 1368   23
[4,] 1369   23
[5,] 1370   23
[6,] 1371   23
(10080 rows truncated)

These represent pixel coordinates of interest in a jpeg image.  I need
to find the distance from each point to all other points of interest.
The only way I can see to do this is by pythagoras and nested for
loops.

distance-function(x1,y1,x2,y2){sqrt((x2-x1)^2 + (y2-y1)^2)} #pythagoras
for(i in 1:nrow(points)){
for(j in 1:nrow(points)){

dist-c(dist,distance(points[i,1],point.array.indices[i,2],points[j,1],points[j,2]))
}
}

This is obviously prohibitively slow with 1 rows in the array.

Any thoughts on how to do this without for loops? I apologize in
advance if there is an obvious way around this.

Thanks!

Andrew Barr
University of Texas at Austin

__
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] isolate elements in vector that match one of many possible values

2008-09-08 Thread Andrew Barr
Hi all,

I want to get the index numbers of all elements of a vector which match any
of a long series of possible values.  Say x - c(1,2,3,4) and I want to know
which values are equal to 1, 2 or 4.  I could do

which(x == 1 | x==2 | x==4)
[1] 1 2 4

This gets really ugly though, when the list of values of interest is really
long.  Is there a nicer way to do this?  Something akin to the MySQL
construction in(), as in

#MySQL script example
Select * from table where parameter in(x,y,z);

Thanks!

-- 
W. Andrew Barr
Biological Anthropology
University of Texas at Austin

[[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] using complete.cases() with nested factors

2008-09-04 Thread Andrew Barr
Hello,

This maybe a newbie question.  I have a dataframe that looks like the sample
at the bottom of the email.  I have monthly precipitation data from several
sites over several years.  For each site, I need to extract years that have
a complete series of 12 monthly precipitation values, while excluding that
year for sites with incomplete data.  I can't figure out how to do this
gracefully (i.e. without a silly for loop).  Any help will be appreciate,
thanks!

Andrew

SiteIDyearmonthprecip(mm)
6700901941jan2998
6700901941feb1299
6700901941mar1007
6700901941apr354
6700901941may88
6700901941jun156
6700901941jul8
6700901941aug4
6700901941sep8
6700901941oct58
6700901941nov397
6700901941dec248
6700901942janNA
6700901942feb380
6700901942mar797
6700901942apr142
6700901942may43
6700901942jun14
6700901942jul70
6700901942aug51
6700901942sep0
6700901942oct10
6700901942nov235
6700901942dec405

-- 
Andrew

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