Posted By: Anonymous
Does anyone know how to remove an entire column from a data.frame in R? For example if I am given this data.frame:
> head(data) chr genome region 1 chr1 hg19_refGene CDS 2 chr1 hg19_refGene exon 3 chr1 hg19_refGene CDS 4 chr1 hg19_refGene exon 5 chr1 hg19_refGene CDS 6 chr1 hg19_refGene exon
and I want to remove the 2nd column.
You can set it to
> Data$genome <- NULL > head(Data) chr region 1 chr1 CDS 2 chr1 exon 3 chr1 CDS 4 chr1 exon 5 chr1 CDS 6 chr1 exon
As pointed out in the comments, here are some other possibilities:
Data <- NULL # Wojciech Sobala Data[] <- NULL # same as above Data <- Data[,-2] # Ian Fellows Data <- Data[-2] # same as above
You can remove multiple columns via:
Data[1:2] <- list(NULL) # Marek Data[1:2] <- NULL # does not work!
Be careful with matrix-subsetting though, as you can end up with a vector:
Data <- Data[,-(2:3)] # vector Data <- Data[,-(2:3),drop=FALSE] # still a data.frame