[R] Drop column from a data frame

John Sorkin jsorkin at grecc.umaryland.edu
Mon Dec 27 02:22:17 CET 2010

I am trying to drop a column of a data frame. The code below attempts to drop a numeric column (which does not work but gives no error or warning) and a factor column (which does not work but gives an error).
I would appreciate someone telling me why my code does not work, and suggesting code that will work.


# create the data frame.
dfxyz <- data.frame(x=1:10,y=11:20,z=factor(c(rep(0,5),rep(1,5))))


# try to drop y column
# does not work, does not produce error message
dfxz <- dfxyz[,-(dfxyz$y)]

# try to drop z column
# does not work, produces error message:
# In Ops.factor(df$z) : - not meaningful for factors
dfxy <- dfxyz[,-dfxyz$z]

John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)

Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}

More information about the R-help mailing list