Hi,
[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;
}
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?
Greetings,
Hedderik.
> version
_
platform i586-unknown-linux
arch i586
os linux
system i586, linux
status
status.rev 0
major 0
minor 63.1
year 1998
month December
day 5
language R
--
http://www.swi.psy.uva.nl/usr/rijn
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._