Message-ID: <C0CF18ED-B8E1-46D8-92BD-404EBE7FA2DA@comcast.net>
Date: 2016-04-08T02:45:26Z
From: David Winsemius
Subject: Using a function with apply Error: undefined columns selected
In-Reply-To: <5706A969020000CB0014FE8B@smtp.medicine.umaryland.edu>
> On Apr 7, 2016, at 3:39 PM, John Sorkin <jsorkin at 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