Skip to content
Prev 255488 / 398506 Next

programming: telling a function where to look for the entered variables

This should be a version that does what you want.
Because you named the variable lvarname, I assumed you were already passing
"lvar" instead of trying to pass lvar (without the quotes), which is in no
way a 'name'.

myfunct.better <- function(subgroup, lvarname, xvarname, yvarname,
dataframe)
{
	#enter the subgroup, the variable names to be used and the dataframe
	#in which they are found
	Data.tmp <- Fulldf[Fulldf[,lvarname]==subgroup,
c(xvarname,yvarname)]
	Data.tmp <-na.omit(Data.tmp)
	indextable <- table(Data.tmp[,xvarname], Data.tmp[,yvarname]) #
create the contingency 
		#table on the basis of the entered variables
	#actually, if I remember well, you could simply use
indextable<-table(Data.tmp) here
	#that would allow for some more simplifications (replace xvarname
and yvarname by
	#columnsOfInterest or similar, and pass that instead of c(xvarname,
yvarname) )
	return(indextable)
}

myfunct.better("yes", lvarname="lvar", xvarname="xvar", yvarname="yvar",
dataframe=Fulldf)


HTH,


Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove




-----Original Message-----
From: irene.prix at googlemail.com [mailto:irene.prix at googlemail.com] On Behalf
Of E Hofstadler
Sent: vrijdag 1 april 2011 14:28
To: Nick Sabbe
Cc: r-help at r-project.org
Subject: Re: [R] programming: telling a function where to look for the
entered variables

Thanks Nick and Juan for your replies.

Nick, thanks for pointing out the warning in subset(). I'm not sure
though I understand the example you provided -- because despite using
subset() rather than bracket notation, the original function (myfunct)
does what is expected of it. The problem I have is with the second
function (myfunct.better), where variable names + dataframe are not
fixed within the function but passed to the function when calling it
-- and even with bracket notation I don't quite manage to tell R where
to look for the columns that related to the entered column names.
(but then perhaps I misunderstood you)

This is what I tried (using bracket notation):

myfunct.better(dataframe, subgroup, lvarname,yvarname){
Data.tmp <- dataframe[dataframe[,deparse(substitute(lvarname))]==subgroup,
c("xvar",deparse(substitute(yvarname)))]
}

but this creates an empty contingency table only -- perhaps because my
use of deparse() is flawed (I think what is converted into a string is
"lvarname" and "yvarname", rather than the column names that these two
function-variables represent in the dataframe)?


2011/4/1 Nick Sabbe <nick.sabbe at ugent.be>:
On
http://www.R-project.org/posting-guide.html