R-2.9.1, Windows7 Dear list, I have a question to you that seems very simple to me, but I just can't figure it out. I have a dataframe called "ratings" which contains the following variables: evalR1, evalR2, evalR3, evalR4, scoreR1, scoreR2, scoreR3, scoreR4, opinionR1, opinionR2, opinionR3, opinionR4. (there are more variables, but this gives an idea of the data structure). What I want is run several analyses on all 3 or 4-combinations of a given variable. So, for example, I want to compute the following ICC's (function from the psych package): ICC(cbind(evalR1,evalR2, evalR3)) ICC(cbind(evalR1,evalR2, evalR4)) ICC(cbind(evalR1, evalR3, evalR4)) ICC(cbind(evalR2, evalR3, evalR4)) ICC(cbind(evalR1, evalR2, evalR3, eval4)). I create a matrix containing the 3-combinations by combn(4,3). Now I need to call the variables into the function. First, I tried paste as follows: combis <- combn(4,3) # this gives the 3-combinations attach(ratings) eval <- paste("evalR",combis[1,1],",evalR",combis[2,1],",evalR","combis[3,1],sep ="") (this is of course just for 1 combination, as an example) the output of this is "evalR1,"evalR2,evalR3", but when I run ICC(cbind(eval)), an error message is given which is not given when I enter ICC(cbind(evalR1,evalR2, evalR3)) manually. The function appears not to recognize the variable names. It also does not work to type ICC(cbind(unquote(eval))). Alternatively, I have tried the cat function, but also here ICC does not recognize the input as variable names. What am I doing wrong? How can I automatically construct the set of variable names such that a function recognizes them as variable names? ICC is one example, but there are also other computations to be run and the set of variables is pretty large, so typing the combinations of variable names manually is really unattractive. What am I missing? It seems to me that there probably is a very simple solution in R, but which? Thank you, Peter Verbeet [[alternative HTML version deleted]]
Hi, It's not enough to create a string with your instructions, it also needs to be evaluated as such. If you really wanted to evaluate your string, you'd need something like, a <- b <- cc <- 1 # dummy example eval(parse(text = "cbind(a, b, cc)")) #library(fortunes) #fortune("parse") but fortune-ately, you probably oughtn't do that. Instead, I think you could use this example, d = data.frame(a1=1:10, b1=sin(1:10), c1 = cos(1:10)) # some data.frame variables = paste(letters[1:3],"1",sep="") # its variable names (alternatively, names(d) would do) ICC = function(x) x # your function ICC(d[ variables[ c(1, 3) ] ]) # apply the function to a subset of the data, by selecting the required columns (If I understood correctly your question) HTH, baptiste 2009/9/7 Helter Two <heltertwo@care2.com>> R-2.9.1, Windows7 > > Dear list, > > I have a question to you that seems very simple to me, but I just can't > figure it out. > I have a dataframe called "ratings" which contains the following > variables: evalR1, evalR2, evalR3, evalR4, scoreR1, scoreR2, scoreR3, > scoreR4, opinionR1, opinionR2, opinionR3, opinionR4. (there are more > variables, but this gives an idea of the data structure). > > What I want is run several analyses on all 3 or 4-combinations of a > given variable. So, for example, I want to compute the following ICC's > (function from the psych package): > ICC(cbind(evalR1,evalR2, evalR3)) > ICC(cbind(evalR1,evalR2, evalR4)) > ICC(cbind(evalR1, evalR3, evalR4)) > ICC(cbind(evalR2, evalR3, evalR4)) > ICC(cbind(evalR1, evalR2, evalR3, eval4)). > > I create a matrix containing the 3-combinations by combn(4,3). Now I > need to call the variables into the function. > First, I tried paste as follows: > combis <- combn(4,3) # this gives the 3-combinations > attach(ratings) > eval <- > paste("evalR",combis[1,1],",evalR",combis[2,1],",evalR","combis[3,1],sep > ="") > (this is of course just for 1 combination, as an example) > the output of this is "evalR1,"evalR2,evalR3", but when I run > ICC(cbind(eval)), an error message is given which is not given when I > enter ICC(cbind(evalR1,evalR2, evalR3)) manually. The function appears > not to recognize the variable names. It also does not work to type > ICC(cbind(unquote(eval))). > Alternatively, I have tried the cat function, but also here ICC does not > recognize the input as variable names. > > What am I doing wrong? How can I automatically construct the set of > variable names such that a function recognizes them as variable names? > ICC is one example, but there are also other computations to be run and > the set of variables is pretty large, so typing the combinations of > variable names manually is really unattractive. > > What am I missing? It seems to me that there probably is a very simple > solution in R, but which? > > Thank you, > Peter Verbeet > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- _____________________________ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag ______________________________ [[alternative HTML version deleted]]
may be this can work testfun<-function(x) { rval="" k<-length(x) for (i in 1: k) rval<-paste(rval,x[i],sep="-") rval } v1<-paste("evalr",1:4,sep="") eval<-expand.grid(w=v1,x=v1,y=v1,z=v1) n<-dim(eval)[1] results<-rep("", n) for (i in 1:n) { row<-unique(unlist(eval[i,])) if (length(row)>=3) results[i]<-testfun(row) } You just have to replace testfun by your own function in this case ICC. Sincerly. Justin BEM BP 1917 Yaoundé Tél (237) 76043774 ________________________________ De : Helter Two <heltertwo@care2.com> À : r-help@r-project.org Envoyé le : Lundi, 7 Septembre 2009, 18h17mn 22s Objet : [R] calling combinations of variable names R-2.9.1, Windows7 Dear list, I have a question to you that seems very simple to me, but I just can't figure it out. I have a dataframe called "ratings" which contains the following variables: evalR1, evalR2, evalR3, evalR4, scoreR1, scoreR2, scoreR3, scoreR4, opinionR1, opinionR2, opinionR3, opinionR4. (there are more variables, but this gives an idea of the data structure). What I want is run several analyses on all 3 or 4-combinations of a given variable. So, for example, I want to compute the following ICC's (function from the psych package): ICC(cbind(evalR1,evalR2, evalR3)) ICC(cbind(evalR1,evalR2, evalR4)) ICC(cbind(evalR1, evalR3, evalR4)) ICC(cbind(evalR2, evalR3, evalR4)) ICC(cbind(evalR1, evalR2, evalR3, eval4)). I create a matrix containing the 3-combinations by combn(4,3). Now I need to call the variables into the function. First, I tried paste as follows: combis <- combn(4,3) # this gives the 3-combinations attach(ratings) eval <- paste("evalR",combis[1,1],",evalR",combis[2,1],",evalR","combis[3,1],sep ="") (this is of course just for 1 combination, as an example) the output of this is "evalR1,"evalR2,evalR3", but when I run ICC(cbind(eval)), an error message is given which is not given when I enter ICC(cbind(evalR1,evalR2, evalR3)) manually. The function appears not to recognize the variable names. It also does not work to type ICC(cbind(unquote(eval))). Alternatively, I have tried the cat function, but also here ICC does not recognize the input as variable names. What am I doing wrong? How can I automatically construct the set of variable names such that a function recognizes them as variable names? ICC is one example, but there are also other computations to be run and the set of variables is pretty large, so typing the combinations of variable names manually is really unattractive. What am I missing? It seems to me that there probably is a very simple solution in R, but which? Thank you, Peter Verbeet [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]