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> dfc1 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
On 18.10.2001 13:46 Uhr, Gary Collins wrote:> 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.... >You might try something like this instead: apply(df, 2, function(x) sum(is.na(x))) Hope this helps Kaspar -- Kaspar Pflugshaupt Geobotanisches Institut Zuerichbergstr. 38 CH-8044 Zuerich Tel. ++41 1 632 43 19 Fax ++41 1 632 12 15 mailto:pflugshaupt at geobot.umnw.ethz.ch privat:pflugshaupt at mails.ch http://www.geobot.umnw.ethz.ch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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> dfc1 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._