[R] Names of data frame columns in an apply

Hedderik van Rijn rijn at swi.psy.uva.nl
Mon Jan 4 15:26:55 CET 1999


[some background]

I have a dataset which describes a number of subjects doing a "scientific
discovery". That is, they have to discover the rules underlying a
particular domain. To do so, they have to set the levels of 5 variables
which leads to a certain outcome.  To identify what kind of "experiments"
the subjects did, I want to combine these levels into one variable
describing the type of experiment they did.

[the problem]

I wrote a function to recode 5 variables into one variable. These 5
variables are stored in a data frame. For example:

subjnr time var1 var2 var3 var4 var5 outcome
1      1    1    0    2    1    0    20
2      1    0    1    2    1    0    20

When I try to recode a data set like this, using (something like):

recode <- function(dataline) <-
  type <- 0;
  if (dataline$var1 == 1) {
    type <- 1;
  if (dataline$var2 == 2) {
    type <- 2;


I don't get what I want, because dataline$var1 always evaluates to NULL.

Is is at all possible to use the $xxx method of refering to columns when
using apply? And if not, is there another way by means of which I can use
column names instead of numbers?


> version
platform   i586-unknown-linux
arch       i586              
os         linux             
system     i586, linux       
status.rev 0                 
major      0                 
minor      63.1              
year       1998              
month      December          
day        5                 
language   R                 


r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list