Search code examples
rselectrow

Using value in Specific Row to Filter the Columns in R


I have to subset a dataframe based on a specific value of a row. This means that if the row containing values greater than 10 must be used as criteria to extract all the column that satisfy the condition in that row.

Here is my data sample.

structure(list(`Copper ores and concentrates; copper mattes, cemen` = c(200.53, 
274.84, 1.37, 376.686907694609), `Fabrics, woven, of man-made fabrics` = c(4093.12, 
1184.47, 0.29, 342.762777758776), Copper = c(44.76, 91.45, 2.04, 
186.843219392315), Zinc = c(80.14, 110.73, 1.38, 152.996417519341
), `Waste, parings and scrap, of plastics` = c(590.3, 286.3, 
0.49, 138.857682534305), `Fixed vegetable fats & oils, crude, refined, fract.` = c(864.14, 
344.63, 0.4, 137.44281817761), `Sulphur and unroasted iron pyrites` = c(23.99, 
55.11, 2.3, 126.599087119633), `Radio-actives and associated materials` = c(48.59, 
76.67, 1.58, 120.977338958633), `Rails & railway track construction mat., iron, steel` = c(464.66, 
214.76, 0.46, 99.259367279301), `Iron ore and concentrates` = c(46.91, 
67.8, 1.45, 97.9927520784481), `Crude vegetable materials, n.e.s.` = c(164.46, 
123.26, 0.75, 92.3812939316551), `Other plastics, in primary forms` = c(187.76, 
124.21, 0.66, 82.169386983383), `Crude animal materials, n.e.s.` = c(43.08, 
56.52, 1.31, 74.1529805013928), `Pig iron & spiegeleisen, sponge iron, powder & granu` = c(17.17, 
33.03, 1.92, 63.5399475829936), `Ores and concentrates of base metals, n.e.s.` = c(15.7, 
27.6, 1.76, 48.5197452229299), `Furskins, tanned or dressed, excluding those of 8483` = c(178.49, 
75.12, 0.42, 31.6152972155303), `Metalworking machinery (excludingmachine-tools) & parts` = c(179.18, 
71.69, 0.4, 28.6832018082375)), row.names = c("SD", "Mean", "INTENSITY", 
"INTENSITY2"), class = "data.frame")

I want that the dataframe must limit itself to values greater than 10 in the row named INTENSITY2.

I tried this tf4[, tf4[,"INTENSITY2" > 10, ]] but it does not work.


Solution

  • This works as well.

    tf4[,unname(apply(tf4['INTENSITY2',],1,function(x) which(x>10)))]