Home > OS >  Pull out column names of cells which match logical criteria
Pull out column names of cells which match logical criteria

Time:07-22

I've got a table such as this:

structure(list(Suggested.Symbol = c("CCT4", "DHRS2", "PMS2", 
"FARSB", "RPL31", "ASNS"), p_onset = c(0.9378, 0.5983, 7.674e-10, 
0.09781, 0.5495, 0.7841), p_dc14 = c(0.3975, 0.3707, 6.117e-17, 
0.2975, 0.4443, 0.7661), p_tfc6 = c(0.2078, 0.896, 7.388e-19, 
0.5896, 0.3043, 0.6696), p_tms30 = c(0.5724, 0.3409, 4.594e-13, 
0.2403, 0.1357, 0.3422)), row.names = c(NA, 6L), class = "data.frame")

I'd like to create a new column called 'summary'. In it, on a row-wise basis, I'd like to return the columns names of the cells with values <0.05, comma separated. Is that possible??

CodePudding user response:

We can use toString by looping over the rows, create a logical vector where the values are less than 0.05, subset the names and paste them with toString

df1$summary <- apply(df1[-1], 1, \(x) toString(names(x)[x < 0.05]))
  • Related