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

filter all variables percentage of samples with a sepcific mean

I have a table of data with multiple samples and variables. I want to filter this table to keep variables (columns) that in > 5% of samples have a value >0. Further i’d like to filter out those variables (not rows) which have a mean >0.2.

structure(list(Samples = 1:29, c01 = c(20.54997, 0, 0, 0, 0.00226, 
0, 0, 0, 0, 65.24078, 0, 0, 0, 0, 0, 0, 0, 12.83106, 0.38555, 
16.45834, 0, 0, 0.00064, 0.005, 0, 0, 0, 12.36956, 0), c02 = c(14.43141, 
0.25318, 0.83121, 0.54282, 8.50687, 0.63261, 1.16257, 0, 0.25563, 
1.55071, 0, 0.65672, 0.44498, 2.13339, 3.01204, 0.25696, 0.10432, 
0.0051, 0.00139, 0.11766, 0.57905, 0, 0.12239, 0, 0, 0, 1.82955, 
0.06883, 0), c03 = c(12.45032, 7.88459, 0.99032, 1.84241, 0.93828, 
2.26896, 3.33106, 8.50696, 5.13512, 1.582, 0.24858, 0.7017, 1.2043, 
2.58593, 1.02544, 0.75702, 1.96771, 5.16734, 3.82884, 1.55069, 
0.97391, 2.47482, 1.06835, 0.01934, 4.41235, 3.68062, 7.01643, 
1.24734, 7.71274), c04 = c(7.5127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0.44856, 3.05223, 7.78115, 0, 0, 6.65269, 0, 0, 4.11219, 0, 
0, 1.00195, 0.00198, 0, 0, 11.43261, 0.00137, 0), c05 = c(6.57903, 
1.45815, 0.28668, 1.66816, 1.66008, 0.77155, 1.00269, 1.03546, 
0.60667, 1.46041, 1.35261, 0.0228, 1.38451, 1.51657, 4.8712, 
5.31965, 1.85348, 1.80051, 0.91537, 3.12064, 3.00647, 0.55887, 
0.02098, 5.04716, 0.66621, 0, 0.10575, 0.89721, 0.04418), c06 = c(3.29649, 
2.45599, 35.45449, 6.43284, 10.45324, 2.58769, 1.32091, 1.75716, 
0.11397, 2.06348, 8.57203, 12.7673, 8.50063, 10.19027, 12.73432, 
3.195, 8.80226, 6.47543, 2.56102, 2.7203, 4.73366, 2.73383, 0, 
0.25893, 4.73464, 0.00275, 5.13602, 18.49032, 0.91792), c07 = c(3.13373, 
0.13374, 1.15662, 0.00969, 0, 0.95451, 0.35991, 0.80672, 0, 0, 
2.07513, 0, 1.14085, 0, 0.19192, 0.16836, 0, 0.03299, 0.94231, 
0.49989, 0.32173, 0, 0, 0, 0.01308, 0.41972, 0.00371, 0, 0.19803
), c08 = c(2.42942, 0, 0.05228, 0, 0, 0.12539, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0.14092, 0, 0, 0, 0, 0, 0.06318, 0, 0, 0, 0, 0, 0, 
0), c09 = c(2.08329, 1.48825, 0.3651, 0.04518, 8.95546, 0.50419, 
2.08877, 1.59026, 6.83207, 0.01179, 0.03375, 2.25788, 5.37218, 
1.57857, 1.10407, 0.33614, 1.17234, 0.03309, 0.44883, 1.99542, 
4.34365, 3.09612, 0.64867, 0.00276, 3.6342, 13.58322, 1.21404, 
0.5201, 2.25059), c10 = c(2.01871, 0.16123, 0.17942, 0.01882, 
0, 0.07479, 0.12062, 0.04417, 0.06655, 0.44124, 0.94579, 0.42188, 
0.16252, 0, 0.07415, 0.37692, 0.05604, 0.02513, 0.01643, 0.36103, 
0.11106, 0.00976, 13.86118, 1.00675, 0.08545, 0.05809, 0.00459, 
0.04065, 3.15904), c11 = c(1.80767, 2.60171, 1.52571, 1.42421, 
0.54515, 3.10199, 6.42356, 0.70189, 1.63704, 1.37722, 0.34205, 
1.74508, 1.37933, 0.33491, 5.51428, 0.38707, 0, 3.56014, 3.03788, 
1.36028, 3.95165, 0.45644, 0.001, 0, 0.79523, 0.79666, 0.07401, 
1.9843, 0.1594), c12 = c(1.5703, 0.01536, 0.28295, 0.15292, 0.01971, 
1.43474, 0, 0, 0.0153, 0, 0.0923, 0.43918, 0.09941, 0, 0.20089, 
0.04423, 0, 0.92555, 0.22799, 0.46793, 0.54228, 0.99488, 0, 0, 
0.5476, 0, 0, 0.90751, 0), c13 = c(1.5414, 0.10995, 0.07654, 
0.21751, 0.95283, 0.42834, 0.00731, 0.01768, 0, 0.0078, 0.23865, 
0.19046, 0.3389, 0, 0.09742, 0.01043, 0.04515, 0.04054, 0.01455, 
0.08685, 0.02306, 0, 0, 0, 0.08105, 0, 0.18324, 0.0479, 0.01374
), c14 = c(1.44565, 2.00101, 0.06163, 0.94796, 0.53368, 2.67534, 
0.70291, 0.40036, 1.91345, 2.12895, 1.12242, 1.94325, 3.10206, 
1.99831, 1.43953, 0.12424, 0.49666, 0.31405, 0.10889, 2.56575, 
1.76696, 3.09154, 0, 6.71776, 1.80677, 0.31812, 0.04629, 1.62853, 
0.53896), c15 = c(1.42826, 0.4252, 0.00057, 0.10527, 0, 0, 1.34473, 
0.28319, 0.06461, 0, 0, 0, 0, 0, 0.60115, 0, 0, 2.88822, 0.45054, 
0, 0.02099, 0, 0, 0, 0, 0, 0, 1.36053, 0), c16 = c(1.42179, 0, 
0, 0, 0, 0, 0, 0, 0, 0.04243, 0, 0, 0, 0, 0, 0.53077, 0.37179, 
0, 0.16018, 1.09049, 0, 0, 0, 0, 0.5307, 0, 0, 0, 1.92085), c17 = c(1.24998, 
0.62542, 0.79644, 0.07306, 0.05504, 0.16539, 0.42176, 0.21835, 
0.03788, 0.20085, 0.27482, 0, 0.5395, 0, 0.81909, 1.48779, 1.01917, 
0.17791, 0.3074, 0.66899, 0.31369, 0.09243, 0, 2.60673, 0.22892, 
0, 0.04191, 0.44308, 2.61117)), class = "data.frame", row.names = c(NA, 
-29L))

>Solution :

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

library(dplyr)
df |> 
  select(where(\(x) mean(x > 0) > 0.05) & where(\(x) mean(x) > 0.2))

I’d suggest having a look at the ?where help page for more examples and details.

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