Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-06 Thread Rui Barradas
Hello, Just to say that I wouldn't write the function as John did. I would get rid of all the deparse/substitute stuff and instinctively use a quoted argument as a column name. Something like the following. myfun <- function(frame, var){ [...] col <- frame[, var] # or frame[[

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread Bert Gunter
Typo: "lazy evaluation" not "lay evaluation." -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Dec 5, 2016 at 1:46 PM, Bert Gunter wrote: > Sorr

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread Bert Gunter
Sorry, hit "Send" by mistake. Inline. On Mon, Dec 5, 2016 at 1:34 PM, Bert Gunter wrote: > Inline. > > -- Bert > > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" com

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread Bert Gunter
Inline. -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Dec 5, 2016 at 9:53 AM, Rui Barradas wrote: > Hello, > > Inline. > > Em 05-12-2016 17:09

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread David Winsemius
> On Dec 5, 2016, at 9:53 AM, Rui Barradas wrote: > > Hello, > > Inline. > > Em 05-12-2016 17:09, David Winsemius escreveu: >> >>> On Dec 5, 2016, at 7:29 AM, John Sorkin wrote: >>> >>> Rui, >>> I appreciate your suggestion, but eliminating the deparse statement does >>> not solve my probl

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread Rui Barradas
Hello, Inline. Em 05-12-2016 17:09, David Winsemius escreveu: On Dec 5, 2016, at 7:29 AM, John Sorkin wrote: Rui, I appreciate your suggestion, but eliminating the deparse statement does not solve my problem. Do you have any other suggestions? See code below. Thank you, John mydf <- dat

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread Rui Barradas
Hello, For some reason I got moderation in reply to Bert's post so I'll retry. Get rid of 'xx', keep 'yy': frame[, yy] # this works Hope this helps, Rui Barradas Em 05-12-2016 15:29, John Sorkin escreveu: Rui, I appreciate your suggestion, but eliminating the deparse statement does not solv

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread Rui Barradas
Bert is right, that's exactly what I mant. Rui Barradas Em 05-12-2016 15:36, Bert Gunter escreveu: John: I think you need to re-read about how functions pass arguments and data frame access works in R. What Rui meant was to get rid of all the xx stuff and access your column by: col <- frame[

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread David Winsemius
> On Dec 5, 2016, at 7:29 AM, John Sorkin wrote: > > Rui, > I appreciate your suggestion, but eliminating the deparse statement does not > solve my problem. Do you have any other suggestions? See code below. > Thank you, > John > > > mydf <- > data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F"

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread Bert Gunter
John: I think you need to re-read about how functions pass arguments and data frame access works in R. What Rui meant was to get rid of all the xx stuff and access your column by: col <- frame[, yy] That *does* work. Cheers, Bert Bert Gunter "The trouble with having an open mind is that pe

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread John Sorkin
Rui, I appreciate your suggestion, but eliminating the deparse statement does not solve my problem. Do you have any other suggestions? See code below. Thank you, John mydf <- data.frame(id=c(1,2,3,4,5),sex=c("M","M","M","F","F"),age=c(20,34,43,32,21)) mydf class(mydf) myfun <- function(frame,

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread Rui Barradas
I forgot to say that I've commented out the line # This does not work. #col <- xx[,"yy"] Rui Barradas Em 05-12-2016 15:17, Rui Barradas escreveu: Hello, You don't need xx <- deparse(substitute(...)), since you are passing the data.frame to your function. Just use myfun <- function(frame,var

Re: [R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread Rui Barradas
Hello, You don't need xx <- deparse(substitute(...)), since you are passing the data.frame to your function. Just use myfun <- function(frame,var){ [...] # Nor does this work. col <- frame[,yy] print(col) } myfun(mydf,age) myfun(frame = mydf, var = age) [1] 2 3 I can get the name o

[R] Write a function that allows access to columns of a passed dataframe.

2016-12-05 Thread John Sorkin
I am trying to write a function which, when passed the name of a dataframe and the name of a column of the dataframe, will allow me to work on the columns of a dataframe. I can not get my code to work. Please see the code below. Any help in getting the function to work would be appreciated.