[R] eval and as.symbol question

Gary Collins gco at eortc.be
Thu Oct 18 13:46:38 CEST 2001


Dear all,
Can anyone help with the following problem...

if c1,...,c5 were objects and I wanted to simply count the number of missing items I can simply do

example1 <- function ()
{
        xx_vector(length=5))
        for(i in 1:5){
                xx[i]_sum(is.na(eval(as.symbol(paste("c", i, sep="")))==TRUE))
      }
      xx
}
This prodcues the correct results...
But when I merge these into a dataframe with colnames(df)<- c("c1","c2","c3","c4","c5"), so that
> df
  c1 c2 c3 c4 c5
1  1  1 NA NA NA
2  2  2 NA NA NA
3  3  3 NA NA NA
4 NA NA NA  1  1
5  2  2  2  2  2
6  2  2  2  2  2
7 NA  3  3 NA  2

and try the next "logical" step from above....

example2 <- function (x) 
{
        xx_matrix(ncol=1, nrow=5)
        for(i in 1:5){
                xx[i] <- sum(is.na(eval(as.symbol(paste("x$c", i, sep="")))==TRUE))
      }
      xx
}
> count.missing(x)
Error in eval(expr, envir, enclos) : Object "x$c1" not found

Any suggestions...? The counting missing data was merely an example, I want to try and understand this process so as to implement the idea into another function I am writing dealing with dataframes.
Thanks
Gary.
__________________________________________________
Dr. Gary S. Collins,
Statistics Research Fellow,
Quality of Life Unit, 
European Organisation for Research and Treatment of Cancer, 
EORTC Data Center, 
Avenue E. Mounier 83, bte. 11,
B-1200 Brussels, Belgium.

Tel: +32 2 774 1 606
Fax: +32 2 779 4 568
http://www.eortc.be/home/qol/
__________________________________________________


-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://stat.ethz.ch/pipermail/r-help/attachments/20011018/ba09cd0d/attachment.html


More information about the R-help mailing list