[R] parse and eval character vector

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue Aug 26 10:49:29 CEST 2008


Just use as.numeric. Non numeric will be NA. So the solution of your
problem is na.omit(as.numeric(temp1))

HTH,

Thierry


------------------------------------------------------------------------
----
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
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Rob Foxall
Verzonden: dinsdag 26 augustus 2008 10:36
Aan: r-help op r-project.org
Onderwerp: [R] parse and eval character vector

Dear R-help,

I have a character vector, some elements will be numeric, some not,
and some even empty. E.g.:

temp1 <- c("abcd","  2 ","")

I'm only interested in the numeric elements, the rest I can just throw
away. It is easy enough to loop through the vector:

temp <- try(eval(parse(text=temp1[1])), silent=TRUE); class(temp) #
try-error
temp <- try(eval(parse(text=temp1[2])), silent=TRUE); class(temp) #
numeric
temp <- try(eval(parse(text=temp1[3])), silent=TRUE); class(temp) # NULL

and then throw away the non-numeric/NULL stuff. But, as this vector
will be long, I would really like to speed things up by not using a
loop, and I thought that "lapply" might do the trick. However:

temp.fn <- function(x)
  try(eval(parse(text=x)), silent=TRUE)

temp2 <- lapply(temp1, FUN=temp.fn)
class(temp2[2]) # list, for elements 1, 2, and 3

and I don't know how to extract the numeric elements from here. So,
can I either use lapply as above and somehow get the information I
need out of "temp2" (I've tried using "unlist" but had no success), or
is there some other function that I can apply to my character vector
to avoid looping?

Rob.

______________________________________________
R-help op 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.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in  this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document



More information about the R-help mailing list