Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k 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

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]  

 

Output:

    chr      region

1  chr1        CDS

2  chr1       exon

3  chr1        CDS

4  chr1       exon

5  chr1        CDS

6  chr1       exon

Browse Categories

...