> On Apr 7, 2016, at 3:39 PM, John Sorkin <jsor...@grecc.umaryland.edu> wrote:
> 
> I am trying to write a function that can be used to apply to process all the 
> columns of a data.frame. If you will run the code below, you will get the 
> error message undefined columns selected. I hope someone will be able to 
> teach me what I am doing wrong.
> Thank you,
> John 
> 
> # create data frame.
> guppy
> 
> fract2 <- function(col,data) {
>  cat("Prove we have passed the data frame\n")
>  print(data)
> 
>  # Get the name of the column being processed.
>  zz<-deparse(substitute(col))
>  cat("Column being processed\n")
>  print(zz)
>  p<-sum(data[,zz]!="")/length(data[,zz])
>  return(p)
> }
> 
> apply(guppy,2,fract2,data=guppy)

At the point where the error is about to occur during the first column being 
processed, this is what had been printed:

Column being processed
[1] "newX[, i]"

So it should be no surprise that the actual error was:

Error in `[.data.frame`(data, , zz) : undefined columns selected

It occurred at one of the two points where you tried `data[,zz]`

You need to pass colnames(guppy) to fract2 and work with the character values.

All of the *apply function pass only values so they do not pass the column 
names for testing. A possible exception might be the colnames of a vector being 
avaialble when using apply with an index of 1.

-- 



> John David Sorkin M.D., Ph.D.
> 
Snipped

-- 
David Winsemius
Alameda, CA, USA

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to