Search code examples
rdataframematrixdevtools

Keep Rows of Dataframe that have same column name of matrix R


I want to keep the rows of a dataframe (20 rows) of matrix that only has 13 columns, meaning to eliminate the rows of my dataframe that have the same name as the column names of my matrix. Thus end up with a final dataframe of 13 rows, this because I want to annotate this dataframe to my matrix when plotting a heatmap in the future.

I'm not sure if this is actually possible :S

Matrix: enter image description here

DataFrame:

enter image description here


Solution

  • Sample Data:

    Mydataframe <- data.frame(casecontrol = c(rep("case",10),rep("Control",10)),
                              condition=c(rep("cond1",5),rep("cond2",5),rep("cond3",5),rep("cond4",5)))
    row.names(Mydataframe) <- sapply(1:20, function(x) paste0("sample",x))
    
    Mymatrix <- matrix(0,nrow=10,ncol=13)
    colnames(Mymatrix) <- sapply(1:13, function(x) paste0("sample",x))
    

    You can find the matches and remove them as follows:

    RowsToRemove <- match(colnames(Mymatrix),row.names(Mydataframe))
    MyNewdataframe <- Mydataframe[-RowsToRemove,]
    

    output

    > MyNewdataframe
             casecontrol condition
    sample14     Control     cond3
    sample15     Control     cond3
    sample16     Control     cond4
    sample17     Control     cond4
    sample18     Control     cond4
    sample19     Control     cond4
    sample20     Control     cond4