0 votes
1 view
in R Programming by (6.5k points)

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.

1 Answer

0 votes
by (25.3k points)

To remove an entire column from a data frame, you can use any one of the following ways

Data[2] <- NULL

Data[[2]] <- NULL 

Data <- Data[,-2] 

Data <- Data[-2]  



    chr      region

1  chr1        CDS

2  chr1       exon

3  chr1        CDS

4  chr1       exon

5  chr1        CDS

6  chr1       exon

Welcome to Intellipaat Community. Get your technical queries answered by top developers !