I've been trying to wrap my head around this one... I have a table of gene expression values that are greater than a certain value, so each is a conditional value (TRUE/FALSE). I want to, for each row, return the column names for the cells that are TRUE. Afterwards, I want a table with the row numbers as column headers, and the column names for TRUE cells in each column. Or alternatively, just a list of 'TRUE' column names for each row number
My input:
> sc_mydata > 0.5
Slc6a14 / 1420504_at Calca / 1452004_at Ms4a4d / 1418990_at Hsd17b14 / 1429802_at 1432227_at
[1,] FALSE FALSE FALSE TRUE TRUE
[2,] FALSE TRUE FALSE FALSE TRUE
[3,] FALSE FALSE FALSE FALSE FALSE
[4,] TRUE FALSE TRUE FALSE FALSE
I tried to use a for loop, but it doesn't give me any column names unfortunately. Anyone able to help...?!
CodePudding user response:
How about this:
mat <- matrix(c(FALSE, FALSE, FALSE, TRUE, TRUE,
FALSE, TRUE, FALSE, FALSE, TRUE,
FALSE, FALSE, FALSE, FALSE, FALSE,
TRUE, FALSE, TRUE, FALSE, FALSE),
ncol=5)
colnames(mat) <- c("Slc6a14 / 1420504_at", "Calca / 1452004_at", "Ms4a4d / 1418990_at", "Hsd17b14 / 1429802_at", "1432227_at")
apply(mat, 1, function(x)colnames(mat)[which(x)])
#> [[1]]
#> [1] "Calca / 1452004_at"
#>
#> [[2]]
#> [1] "Ms4a4d / 1418990_at" "1432227_at"
#>
#> [[3]]
#> [1] "Calca / 1452004_at"
#>
#> [[4]]
#> [1] "Slc6a14 / 1420504_at" "Hsd17b14 / 1429802_at"
Created on 2022-11-24 by the reprex package (v2.0.1)