{"id":16051,"date":"2022-10-14T01:28:45","date_gmt":"2022-10-13T19:58:45","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/"},"modified":"2022-10-14T01:28:45","modified_gmt":"2022-10-13T19:58:45","slug":"solved-subset-columns-in-r-with-specific-values","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/","title":{"rendered":"[Solved] Subset columns in R with specific values"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-28214535\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"28214535\" data-parentid=\"28214062\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Assuming you mean subset columns that all of their values are greater than 1 you could do something like this (essentially you can also use the following according to any condition you might have. Just change the <code>if<\/code> condition):<\/p>\n<p><strong>Example Data<\/strong><\/p>\n<pre><code>a &lt;- data.frame(matrix(runif(90),ncol=3))\n\n&gt; a\n           X1         X2         X3\n1  0.33341130 0.09307143 0.51932506\n2  0.78014395 0.30378432 0.67309736\n3  0.19967771 0.30829771 0.60144888\n4  0.77736355 0.42504910 0.23880491\n5  0.60631868 0.55198423 0.29565519\n6  0.24246456 0.57945721 0.17882712\n7  0.10499677 0.48768998 0.54931955\n8  0.92288335 0.29290491 0.72885160\n9  0.85246128 0.87564673 0.60069170\n10 0.39931205 0.29895856 0.83249469\n11 0.33674259 0.85618041 0.62940935\n12 0.27816980 0.51508938 0.76079354\n13 0.19121182 0.27586235 0.21273823\n14 0.66337625 0.18631150 0.67762964\n15 0.00923405 0.84753915 0.08386400\n16 0.33209371 0.54919903 0.49128825\n17 0.97685675 0.25564765 0.56439142\n18 0.26710042 0.75852884 0.88706946\n19 0.32422355 0.58971620 0.84070049\n20 0.73000898 0.09068726 0.92541277\n21 0.80547283 0.93723241 0.31050230\n22 0.28897215 0.80679092 0.06080124\n23 0.32190269 0.12254342 0.42506740\n24 0.52569405 0.68506407 0.68302356\n25 0.31098388 0.66225007 0.08565480\n26 0.67546897 0.08123716 0.58419470\n27 0.29501987 0.17836528 0.79322116\n28 0.20736102 0.81145297 0.44078101\n29 0.75165829 0.51865202 0.36653840\n30 0.63375066 0.03804626 0.69949846\n<\/code><\/pre>\n<p><strong>Solution<\/strong><\/p>\n<p>Just a single <code>lapply<\/code> is enough. I use <code>0.05<\/code> as threshold here because it is easier to demonstrate how to use it according to my random data set. Change that to whatever you want in your dataset.<\/p>\n<pre><code>b &lt;- do.call(cbind, (lapply(a, function(x) if(all(x&gt;0.05)) return(x) )))\n<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code>&gt; b\n              X3\n [1,] 0.51932506\n [2,] 0.67309736\n [3,] 0.60144888\n [4,] 0.23880491\n [5,] 0.29565519\n [6,] 0.17882712\n [7,] 0.54931955\n [8,] 0.72885160\n [9,] 0.60069170\n[10,] 0.83249469\n[11,] 0.62940935\n[12,] 0.76079354\n[13,] 0.21273823\n[14,] 0.67762964\n[15,] 0.08386400\n[16,] 0.49128825\n[17,] 0.56439142\n[18,] 0.88706946\n[19,] 0.84070049\n[20,] 0.92541277\n[21,] 0.31050230\n[22,] 0.06080124\n[23,] 0.42506740\n[24,] 0.68302356\n[25,] 0.08565480\n[26,] 0.58419470\n[27,] 0.79322116\n[28,] 0.44078101\n[29,] 0.36653840\n[30,] 0.69949846\n<\/code><\/pre>\n<p>Only column 3 confirmed the condition on this occasion so it was returned.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Subset columns in R with specific values <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Assuming you mean subset columns that all of their values are greater than 1 you could do something like this (essentially you can also use the following according to any condition you might have. Just change the if condition): Example Data a &lt;- data.frame(matrix(runif(90),ncol=3)) &gt; a X1 X2 X3 1 0.33341130 0.09307143 0.51932506 2 &#8230; <a title=\"[Solved] Subset columns in R with specific values\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/\" aria-label=\"More on [Solved] Subset columns in R with specific values\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[321],"class_list":["post-16051","post","type-post","status-publish","format-standard","hentry","category-solved","tag-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Subset columns in R with specific values - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Subset columns in R with specific values - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Assuming you mean subset columns that all of their values are greater than 1 you could do something like this (essentially you can also use the following according to any condition you might have. Just change the if condition): Example Data a &lt;- data.frame(matrix(runif(90),ncol=3)) &gt; a X1 X2 X3 1 0.33341130 0.09307143 0.51932506 2 ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-13T19:58:45+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Subset columns in R with specific values\",\"datePublished\":\"2022-10-13T19:58:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/\"},\"wordCount\":106,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"r\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/\",\"name\":\"[Solved] Subset columns in R with specific values - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-13T19:58:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Subset columns in R with specific values\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Subset columns in R with specific values - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Subset columns in R with specific values - JassWeb","og_description":"[ad_1] Assuming you mean subset columns that all of their values are greater than 1 you could do something like this (essentially you can also use the following according to any condition you might have. Just change the if condition): Example Data a &lt;- data.frame(matrix(runif(90),ncol=3)) &gt; a X1 X2 X3 1 0.33341130 0.09307143 0.51932506 2 ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/","og_site_name":"JassWeb","article_published_time":"2022-10-13T19:58:45+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Subset columns in R with specific values","datePublished":"2022-10-13T19:58:45+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/"},"wordCount":106,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["r"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/","url":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/","name":"[Solved] Subset columns in R with specific values - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-13T19:58:45+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-subset-columns-in-r-with-specific-values\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Subset columns in R with specific values"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16051"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16051\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}