Names of data frame columns in an apply
Hedderik van Rijn <rijn at swi.psy.uva.nl> writes:
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;
}
type
}
apply(data,1,recode)
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?
You cannot use $ because the argument to recode is not a list, it is a (non-generic) vector. Cf.
dd<-data.frame(a=1:2,b=3:4) apply(dd,1,print)
a b 1 3 a b 2 4 1 2 a 1 2 b 3 4
apply(dd,1,mode)
1 2 "numeric" "numeric" However, dataline["var1"] should work. But are you sure this is the way to go? How about something like: transform(dframe, type=ifelse(var1==1,1,ifelse(var2==2,2,0)))
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._