Polwart Calum (County Durham and Darlington NHS Foundation Trust)
2009-Aug-06 19:50 UTC
[R] Using 'field names' of a data.frame in a function
I may be doing this wrong! but I have a function which I have simplified a lot
below. I want to pass some 'field names' of a data-frame to the
function for it to then do some manipulation of.
Here's my code:
#build a simple dataset
mydataset = data.frame (
ages=c('40-49','40-49','40-49','30-39','50-59','50-59','60-69','50-59'),
sex =
c("M","F","F","M","F","F","M","M"))
#build a simple function
myfunc <- function (categories) {
table (categories)
}
#call the function
myfunc (c(mydataset$ages, mydataset$sex))
==
My output I am getting is:
categories
1 2 3 4
5 7 3 1
But what I'm expecting is:
table (mydataset$ages, mydataset$sex)
F M
30-39 0 1
40-49 2 1
50-59 2 1
60-69 0 1
Calling the function as: myfunc ("mydataset$ages, mydataset$sex")
doesn't work either and nor does myfunc (c("mydataset$ages",
"mydataset$sex"))
Now in the simple version above I could make the function (category1, category2)
and then call table (category1, category2) - but what if I might have a category
3, category 4 etc... How can I pass the dataset name to the function without it
trying to actually pass the data to the function? I've also tried paste -
but perhaps I'm mis-using it?
Many thanks for help in advance
********************************************************************************************************************
This message may contain confidential information. If yo...{{dropped:21}}
Henrique Dallazuanna
2009-Aug-06 20:04 UTC
[R] Using 'field names' of a data.frame in a function
Try this: myfunc <- function(...)table(...) with(mydataset, myfunc(ages, sex)) On Thu, Aug 6, 2009 at 4:50 PM, Polwart Calum (County Durham and Darlington NHS Foundation Trust) <calum.polwart@nhs.net> wrote:> I may be doing this wrong! but I have a function which I have simplified a > lot below. I want to pass some 'field names' of a data-frame to the > function for it to then do some manipulation of. > > Here's my code: > #build a simple dataset > mydataset = data.frame ( > ages=c('40-49','40-49','40-49','30-39','50-59','50-59','60-69','50-59'), > sex = c("M","F","F","M","F","F","M","M")) > > #build a simple function > myfunc <- function (categories) { > table (categories) > } > > #call the function > myfunc (c(mydataset$ages, mydataset$sex)) > > ==> > My output I am getting is: > categories > 1 2 3 4 > 5 7 3 1 > > But what I'm expecting is: > table (mydataset$ages, mydataset$sex) > > F M > 30-39 0 1 > 40-49 2 1 > 50-59 2 1 > 60-69 0 1 > > > Calling the function as: myfunc ("mydataset$ages, mydataset$sex") doesn't > work either and nor does myfunc (c("mydataset$ages", "mydataset$sex")) > > Now in the simple version above I could make the function (category1, > category2) and then call table (category1, category2) - but what if I might > have a category 3, category 4 etc... How can I pass the dataset name to the > function without it trying to actually pass the data to the function? I've > also tried paste - but perhaps I'm mis-using it? > > Many thanks for help in advance > > > ******************************************************************************************************************** > > This message may contain confidential information. If ...{{dropped:20}}