Search code examples
rdataframestatisticsrowscolumnsorting

extract specefic colum based on character value in a row


I have a data that contains character in the first row like this

       J   K    L   M  N  O  P
A      T   F    T   F  F  F  T   
B     14  15   10   2  3  4  78
C     10  47   15   9  6 12  12 
D     17  44   17   1  0 15  11
E      3  12   14   3  2 15  17

i want to extract only the columns that contain the value "T" in row A

so the result i want is this :

       J      L     P
A      T      T     T   
B     14     10    78
C     10     15    12 
D     17     17    11
E      3     14    17

also, in second time, i want to know how to do the same thing using two conditions, for example : extract all columns that contain value "T" in column A and value 17 in row D so the result will be :

       J      L     
A      T      T      
B     14     10    
C     10     15    
D     17     17  
E      3     14    

Thank you


Solution

  • Here is your answer.

    > df <- df[, df["A",] == "T" & df["D",] == 17]
    

    You can use index to filter columns. It supports logical statements and you can combine them with &.