I would like to specify that certain columns in a data frame should be treated as ordered factors. I know what numbers these columns are, but not their names. How do I do this? For example, if I know columns 1:4 are to be treated as factors, I can write dat <- matrix(c(2,1,1,1, 1,1,1,1), 2, 4) D <- as.data.frame(dat) # force all variables to be treated as binary # regardless of the small data set D$V1 <- factor(D$V1, 1:2) D$V2 <- factor(D$V2, 1:2) D$V3 <- factor(D$V3, 1:2) D$V4 <- factor(D$V4, 1:2) But how do I do this in general? What I would like to say is something like for (i in my.factor.columns) { D$Vi <- factor(D$Vi, 1:my.nlevels[i]) } Presumably I could do something tricky using eval, but I don't know how. Besides, I'd prefer to avoid eval, since it is slow. (Also, I don't want to rely on the fact that the columns are named "Vnn" by default.) Kevin -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Robert.Denham@dnr.qld.gov.au
2001-Jul-17 05:36 UTC
[R] variable number of variables in data frames
> For example, if I know columns 1:4 are to be treated as factors, I can > write> dat <- matrix(c(2,1,1,1, 1,1,1,1), 2, 4) > D <- as.data.frame(dat)> # force all variables to be treated as binary >.# regardless of the small data set > D$V1 <- factor(D$V1, 1:2) > D$V2 <- factor(D$V2, 1:2) > D$V3 <- factor(D$V3, 1:2) > D$V4 <- factor(D$V4, 1:2)> But how do I do this in general? What I would like to say is something > likeWhy not just for (i in my.factor.columns) { # D$Vi <- factor(D$Vi, 1:my.nlevels[i]) D[,i]<- factor(D[,i], 1:my.nlevels[i]) } Robert ************************************************************************ The information in this e-mail together with any attachments is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any form of review, disclosure, modification, distribution and/or publication of this e-mail message is prohibited. If you have received this message in error, you are asked to inform the sender as quickly as possible and delete this message and any copies of this message from your computer and/or your computer system network. ************************************************************************ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Henrik Bengtsson
2001-Jul-17 06:16 UTC
[R] RE: variable number of variables in data frames
Kevin, you can treat data frames as matrices, i.e. you can do dat <- matrix(c(2,1,1,1, 1,1,1,1), 2, 4) D <- as.data.frame(dat) for (i in 1:ncol(D)) D[,i] <- factor(D[,i], 1:2) Henrik> -----Original Message----- > From: murphyk at relay.eecs.berkeley.edu > [mailto:murphyk at relay.eecs.berkeley.edu]On Behalf Of Kevin Murphy > Sent: Monday, July 16, 2001 6:19 PM > To: r-help at hypatia.math.ethz.ch; Henrik Bengtsson > Cc: murphyk at cs.berkeley.edu > Subject: variable number of variables in data frames > > > I would like to specify that certain columns in a data frame should be > treated as ordered factors. I know what numbers these columns are, but > not their names. > How do I do this? > > For example, if I know columns 1:4 are to be treated as factors, I can > write > > dat <- matrix(c(2,1,1,1, 1,1,1,1), 2, 4) > D <- as.data.frame(dat) > # force all variables to be treated as binary > # regardless of the small data set > D$V1 <- factor(D$V1, 1:2) > D$V2 <- factor(D$V2, 1:2) > D$V3 <- factor(D$V3, 1:2) > D$V4 <- factor(D$V4, 1:2) > > But how do I do this in general? What I would like to say is something > like > > for (i in my.factor.columns) { > D$Vi <- factor(D$Vi, 1:my.nlevels[i]) > } > > Presumably I could do something tricky using eval, but I don't know how. > Besides, I'd prefer to avoid eval, since it is slow. > (Also, I don't want to rely on the fact that the columns are named "Vnn" > by default.) > > > Kevin >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Kevin Murphy wrote:> I would like to specify that certain columns in a data frame should be > treated as ordered factors. I know what numbers these columns are, but > not their names. > How do I do this? > > For example, if I know columns 1:4 are to be treated as factors, I can > write > > dat <- matrix(c(2,1,1,1, 1,1,1,1), 2, 4) > D <- as.data.frame(dat) > # force all variables to be treated as binary > # regardless of the small data set > D$V1 <- factor(D$V1, 1:2) > D$V2 <- factor(D$V2, 1:2) > D$V3 <- factor(D$V3, 1:2) > D$V4 <- factor(D$V4, 1:2) > > But how do I do this in general? What I would like to say is something > like > > for (i in my.factor.columns) { > D$Vi <- factor(D$Vi, 1:my.nlevels[i]) > } > > Presumably I could do something tricky using eval, but I don't know how. > Besides, I'd prefer to avoid eval, since it is slow. > (Also, I don't want to rely on the fact that the columns are named "Vnn" > by default.) > > KevinWhat about:> dat <- matrix(c(2,1,1,1, 1,1,1,1), 2, 4) > D <- as.data.frame(dat) > makefactor <- function(d,index) { d[index] <- sapply(d[index],factor); return(d)}> # say one wants the two first columns to be factors > index <- c(1,2) > str(makefactor(D,index))Regards, Laurent> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Laurent Gautier CBS, Building 208, DTU PhD. Student D-2800 Lyngby,Denmark tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20010717/e8254a3d/attachment.html