Re: [R] how to use a column name from the data frame in the function

2013-08-22 Thread Steve Lianoglou
Hi, On Thu, Aug 22, 2013 at 9:49 PM, Jeff Newmiller wrote: > Please don't post in HTML format... it messes with code examples. > > Use character indexing (please read the Introduction to R... again if > necessary). > > myf <- function(df, colname){ > df[ ,colname ] > } Or df[[colname]] for da

Re: [R] how to use a column name from the data frame in the function

2013-08-22 Thread Jeff Newmiller
Please don't post in HTML format... it messes with code examples. Use character indexing (please read the Introduction to R... again if necessary). myf <- function(df, colname){ df[ ,colname ] } colname <- "a" myf(m,colname) Until you learn simple R syntax, I strongly recommend avoiding wri

[R] how to use a column name from the data frame in the function

2013-08-22 Thread song song
m=as.data.frame(outer(1:5,6:9)) colnames(m)=c('a','b','c','d') tf=function(df, col){list(mean(eval(substitute(col),df,parent.frame())),col )} tf(m,a) will issue error: Error in tf(m, a) : object 'a' not found How can I replace the col as char 'a' in the function? Thank you [[alternat

[R] how to use a column name from the data frame in the function

2013-08-22 Thread song song
for example I have data frame m as below: m=as.data.frame(outer(1:5,6:9)) colnames(m)=c('a','b','c','d') and I define the function myf=function(df, colname){ suppose colname is a, then: how can I get the column 'a' and how to get the colname as a string, 'a' } Thank you! [[a