Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Wednesday, August 6, 2014

Delete columns of data frame in R

> 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.

E3Cfc<-E3Cfc[,!(names(E3Cfc)=="pk.bound")]

> 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[2] <- NULL    # Wojciech Sobala
Data[[2]] <- 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

Delete a list of elements from data frame in R

This might be an easy question but i still need some help for using R.
I have a data.frame (main_data), lets say..

NAMES   AGE     LOC
Jyo     23      Hyd
Abid    27      Kar
Ras     24      Pun
Poo     25      Goa
Sus     28      Kar
 
I wish to remove a few rows based on a list of names. So lets say I have another list of table as follows:

NAMES_list
Jyo
Ras
Poo
 
So based on this list, if any of the names match to my above "main_data" table, then I would like to remove the whole row contianing them, so the result should be as follows

NAMES   AGE     LOC
Abid    27      Kar
Sus     28      Kar
 
Solution:

Use %in%:

main_data2 <- main_data[ ! main_data$NAMES %in% NAMES_list, ]