1

In R, my dataframe "mydata" has various continuous variable and factors. E.g. "Age" and "IQ" are continuous, "TaskType" and "ClassType" are factorial. A general problem I have in R is that I am not sure how I can refer to a variable by constructing the name from the variable's name's component parts. Apologies in advance, I cannot find the adequate language to describe this (basic?) problem. Here is the example:

The following code works:

by(mydata$Age,mydata$TaskType, stat.desc)

But in order to address a large number of variables/factors I wanted to use something more generic like the following code, which does not work:

variablename="Age"
factorname="TaskType"
by(mydata$variablename,mydata$factorname, stat.desc)
frankieb
  • 41
  • 2
  • 6
    Use `by(mydata[[variablename]], mydata[[factorname]], fun)` – Andrie Sep 02 '16 at 14:50
  • Or `by(mydata[,variablename]],mydata[, factorname], stat.desc)`. – lmo Sep 02 '16 at 14:51
  • where's the duplicate for this ("use [[ instead of $") again ... ? – Ben Bolker Sep 02 '16 at 15:02
  • That works and is really helpful. Thank you for your time. Feel free to let me know if I could have described the problem any better - I am sure others have asked this before but I did not know how to search for it. – frankieb Sep 02 '16 at 15:06
  • @BenBolker This one? Closest I could find trawling the r-faq tag. – Frank Sep 02 '16 at 16:12

0 Answers0