Kurt Wollenberg
2005-Dec-07 17:41 UTC
[R] Maintaining factors when copying from one data frame to another
Greetings all: OK, this is bugging the @#@%* out of me. I know the answer is simple and straightforward but for the life of me I cannot find it in the documentation, in the archives, or in my notes (because I know I've encountered this in the past). My problem is: I have a data frame with columns A, B, C, D, and E. A, B, and E are factors and C and D are numeric. I need a new data frame with just A, C, and D. When I copy these columns to a new data frame>newDF <- data.frame(cbind(oldDF$A, oldDF$C, oldDF$D))all the factor data comes out as levels rather than the original factors. How do I preserve the factors when I copy from one data frame to another? Thanks vary much, Kurt Wollenberg, Ph.D. Tufts Center for Vision Research Tufts-New England Medical Center 750 Washington St #450 Boston, MA 02111 Office: 617-636-9028 Fax: 617-636-8945 email: kurt.wollenberg at gmail dot com
Kevin E. Thorpe
2005-Dec-07 17:57 UTC
[R] Maintaining factors when copying from one data frame to another
Does newDF <- oldDF[,c("A","C","D")] work? Kurt Wollenberg wrote:>Greetings all: > >OK, this is bugging the @#@%* out of me. I know the answer is simple >and straightforward but for the life of me I cannot find it in the >documentation, in the archives, or in my notes (because I know I've >encountered this in the past). My problem is: > >I have a data frame with columns A, B, C, D, and E. A, B, and E are >factors and C and D are numeric. I need a new data frame with just A, >C, and D. When I copy these columns to a new data frame > > > >>newDF <- data.frame(cbind(oldDF$A, oldDF$C, oldDF$D)) >> >> > >all the factor data comes out as levels rather than the original >factors. How do I preserve the factors when I copy from one data frame >to another? > > >Thanks vary much, >Kurt Wollenberg, Ph.D. >Tufts Center for Vision Research >Tufts-New England Medical Center >750 Washington St #450 >Boston, MA 02111 >Office: 617-636-9028 >Fax: 617-636-8945 >email: kurt.wollenberg at gmail dot com > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >-- Kevin E. Thorpe Biostatistician/Trialist, Knowledge Translation Program Assistant Professor, Department of Public Health Sciences Faculty of Medicine, University of Toronto email: kevin.thorpe at utoronto.ca Tel: 416.946.8081 Fax: 416.946.3297
Ferdinand Alimadhi
2005-Dec-07 17:59 UTC
[R] Maintaining factors when copying from one data frame to another
newDF <- oldDF[,c("A","C","D")] HTH Kurt Wollenberg wrote:>Greetings all: > >OK, this is bugging the @#@%* out of me. I know the answer is simple >and straightforward but for the life of me I cannot find it in the >documentation, in the archives, or in my notes (because I know I've >encountered this in the past). My problem is: > >I have a data frame with columns A, B, C, D, and E. A, B, and E are >factors and C and D are numeric. I need a new data frame with just A, >C, and D. When I copy these columns to a new data frame > > > >>newDF <- data.frame(cbind(oldDF$A, oldDF$C, oldDF$D)) >> >> > >all the factor data comes out as levels rather than the original >factors. How do I preserve the factors when I copy from one data frame >to another? > > >Thanks vary much, >Kurt Wollenberg, Ph.D. >Tufts Center for Vision Research >Tufts-New England Medical Center >750 Washington St #450 >Boston, MA 02111 >Office: 617-636-9028 >Fax: 617-636-8945 >email: kurt.wollenberg at gmail dot com > >______________________________________________ >R-help@stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Ferdinand Alimadhi Programmer / Analyst Harvard University The Institute for Quantitative Social Science (617) 496-0187 falimadhi@latte.harvard.edu www.iq.harvard.edu [[alternative HTML version deleted]]
Tobias Verbeke
2005-Dec-07 18:04 UTC
[R] Maintaining factors when copying from one data frame to another
Kurt Wollenberg wrote:>Greetings all: > >OK, this is bugging the @#@%* out of me. I know the answer is simple >and straightforward but for the life of me I cannot find it in the >documentation, in the archives, or in my notes (because I know I've >encountered this in the past). My problem is: > >I have a data frame with columns A, B, C, D, and E. A, B, and E are >factors and C and D are numeric. I need a new data frame with just A, >C, and D. When I copy these columns to a new data frame > > > >>newDF <- data.frame(cbind(oldDF$A, oldDF$C, oldDF$D)) >> >> > >all the factor data comes out as levels rather than the original >factors. How do I preserve the factors when I copy from one data frame >to another? > > >The following may be better: newDF <- subset(oldDF, select = c(A, C, D)) HTH, Tobias
Roger Bivand
2005-Dec-07 18:04 UTC
[R] Maintaining factors when copying from one data frame to another
On Wed, 7 Dec 2005, Kurt Wollenberg wrote:> Greetings all: > > OK, this is bugging the @#@%* out of me. I know the answer is simple > and straightforward but for the life of me I cannot find it in the > documentation, in the archives, or in my notes (because I know I've > encountered this in the past). My problem is: > > I have a data frame with columns A, B, C, D, and E. A, B, and E are > factors and C and D are numeric. I need a new data frame with just A, > C, and D. When I copy these columns to a new data frame > > >newDF <- data.frame(cbind(oldDF$A, oldDF$C, oldDF$D))set.seed(1) oldDF <- data.frame(A=rnorm(5), B=rnorm(5), C=factor(letters[1:5]), D=rnorm(5)) str(oldDF) newDF <- data.frame(cbind(oldDF$A, oldDF$C, oldDF$D)) str(newDF) # cbind forces to numeric as a matrix, so avoid it here newDFa <- data.frame(Aa=oldDF$A, Ca=oldDF$C, Da=oldDF$D) str(newDFa)> > all the factor data comes out as levels rather than the original > factors. How do I preserve the factors when I copy from one data frame > to another? > > > Thanks vary much, > Kurt Wollenberg, Ph.D. > Tufts Center for Vision Research > Tufts-New England Medical Center > 750 Washington St #450 > Boston, MA 02111 > Office: 617-636-9028 > Fax: 617-636-8945 > email: kurt.wollenberg at gmail dot com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no