Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retrieve columns based on specific row with values that match a substring

I want to retrieve columns where the subtype row has values that start with "1".

library(data.table)
df.1 <- df[df["subtype",] %like% "1",]

Data:

> dput(df[1:10,1:10])
structure(list(TCGA.2Z.A9J1.01A = c("1a", "5.13553076559331", 
"5.25437106109279", "0", "6.94770472476424", "0", "12.413916703553", 
"11.192440344074", "3.1585955515009", "0"), TCGA.2Z.A9J3.01A = c("2a", 
"4.75608390849743", "3.47688712558277", "3.69415587836022", "7.55464180068354", 
"0", "10.1701864662239", "9.9698319970815", "5.3250949389428", 
"0"), TCGA.2Z.A9J6.01A = c("2b", "5.17761487414502", "6.65033806253549", 
"0.584770128557275", "6.94039112615504", "0", "12.0370627256659", 
"10.8210532515207", "5.53845435576078", "0"), TCGA.2Z.A9J8.01A = c("1b", 
"3.73015082384196", "5.42861476811507", "0", "7.92705080275384", 
"0.41565051441473", "11.9979504885468", "9.91529337032864", "5.14529839229733", 
"0"), TCGA.2Z.A9JD.01A = c("2a", "3.30975827142464", "4.33563331445214", 
"0", "8.67290934153811", "0", "10.9513709097776", "10.8023152138843", 
"1.84534953815009", "0"), TCGA.2Z.A9JG.01A = c("2c", "7.28850529763777", 
"3.84460660023553", "3.00626211318484", "5.81778440061722", "0", 
"12.0868373610296", "10.3281796591297", "3.6562103201826", "0"
), TCGA.2Z.A9JJ.01A = c("1c", "6.28339584818368", "0.807272479985472", 
"4.53120676686483", "6.40832124805635", "0", "12.8200019062179", 
"11.792050825269", "2.35741124701605", "0"), TCGA.2Z.A9JL.01A = c("1c", 
"7.03644948442735", "3.73611776175452", "1.34522603534256", "6.5525507570889", 
"0.597983993789946", "12.9645888051835", "11.0094140803487", 
"7.40469964993127", "0"), TCGA.2Z.A9JN.01A = c("1b", "4.17567628766862", 
"0", "0", "7.2921559549592", "0", "12.2914311784018", "10.8173916627083", 
"0.514097004894233", "0"), TCGA.2Z.A9JP.01A = c("1d", "6.3033314339384", 
"1.11789447981141", "1.74010724619747", "7.46591652705797", "0", 
"13.3435017632051", "11.2086677724798", "1.74010724619747", "0"
)), row.names = c("subtype", "A1BG", "A1CF", "A2BP1", "A2LD1", 
"A2ML1", "A2M", "A4GALT", "A4GNT", "AAA1"), class = "data.frame")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We may need to use the logical index as column index instead of row index

df[,df["subtype",] %like% "1"]

-output

   TCGA.2Z.A9J1.01A TCGA.2Z.A9J8.01A  TCGA.2Z.A9JJ.01A  TCGA.2Z.A9JL.01A  TCGA.2Z.A9JN.01A TCGA.2Z.A9JP.01A
subtype               1a               1b                1c                1c                1b               1d
A1BG    5.13553076559331 3.73015082384196  6.28339584818368  7.03644948442735  4.17567628766862  6.3033314339384
A1CF    5.25437106109279 5.42861476811507 0.807272479985472  3.73611776175452                 0 1.11789447981141
A2BP1                  0                0  4.53120676686483  1.34522603534256                 0 1.74010724619747
A2LD1   6.94770472476424 7.92705080275384  6.40832124805635   6.5525507570889   7.2921559549592 7.46591652705797
A2ML1                  0 0.41565051441473                 0 0.597983993789946                 0                0
A2M      12.413916703553 11.9979504885468  12.8200019062179  12.9645888051835  12.2914311784018 13.3435017632051
A4GALT   11.192440344074 9.91529337032864   11.792050825269  11.0094140803487  10.8173916627083 11.2086677724798
A4GNT    3.1585955515009 5.14529839229733  2.35741124701605  7.40469964993127 0.514097004894233 1.74010724619747
AAA1                   0                0                 0                 0                 0                0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading