[R] eval and as.symbol question
Gary Collins
gco at eortc.be
Thu Oct 18 18:43:13 CEST 2001
Problem solved. Hint was from G. Sawitzki (gs at statlab.uni-heidelberg.de)
question to the list about "Parsing for list components".
Solution is
for(i in 1:5){
cat("\n", sum(is.na(eval(parse(text = paste("x$c", i, sep=""))) == TRUE)),
"\n")
}
Again, I want stress, I was not particularly wanting to find the number of
missing items in a list in a dataframe, this was merely an exaxmple I was
trying to figure out using eval, parse etc...
I wanted an approach to access certain (non-adjacent) columns in a dataframe
using an identifier c1,c2,... for example.
Thanks
Gary
-----Original Message-----
From: Gary Collins
To: r-help
Sent: 10/18/01 1:46 PM
Subject: [R] eval and as.symbol question
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/ <http://www.eortc.be/home/qol/>
__________________________________________________
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list