{"id":943,"date":"2023-07-28T05:49:15","date_gmt":"2023-07-28T05:49:15","guid":{"rendered":"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/"},"modified":"2023-07-28T05:49:15","modified_gmt":"2023-07-28T05:49:15","slug":"filtrar-linhas-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/","title":{"rendered":"Como filtrar linhas em r"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Freq\u00fcentemente, voc\u00ea pode estar interessado em um subconjunto de um quadro de dados baseado em certas condi\u00e7\u00f5es em R. Felizmente, isso \u00e9 f\u00e1cil de fazer usando a fun\u00e7\u00e3o <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/filter.html\" target=\"_blank\" rel=\"noopener noreferrer\">filter()<\/a> do pacote <a href=\"https:\/\/dplyr.tidyverse.org\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">dplyr<\/a> .<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>library(dplyr)<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Este tutorial explica v\u00e1rios exemplos de como usar esse recurso na pr\u00e1tica usando o conjunto de dados dplyr integrado chamado <strong>starwars<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#view first six rows of starwars dataset<\/span>\nhead(starwars)\n\n# A tibble: 6 x 13\n  name height mass hair_color skin_color eye_color birth_year gender homeworld\n                                   \n1 Luke~ 172 77 blond fair blue 19 male Tatooine \n2 C-3PO 167 75 &lt;NA&gt; gold yellow 112 &lt;NA&gt; Tatooine \n3 R2-D2 96 32 &lt;NA&gt; white, bl~ red 33 &lt;NA&gt; Naboo    \n4 Dart~ 202 136 none white yellow 41.9 male Tatooine \n5 Leia~ 150 49 brown light brown 19 female Alderaan \n6 Owen~ 178 120 brown, gr~ light blue 52 male Tatooine \n# ... with 4 more variables: species , films , vehicles ,\n# starships \n<\/strong><\/pre>\n<h3> <strong><span style=\"color: #000000;\">Exemplo 1: Filtrar linhas iguais a um determinado valor<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como filtrar o conjunto de dados para linhas em que a vari\u00e1vel &#8220;esp\u00e9cie&#8221; \u00e9 igual a Droid.<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>starwars %&gt;% filter(species == ' <span style=\"color: #008000;\">Droid<\/span> ')\n\n# A tibble: 5 x 13\n  name height mass hair_color skin_color eye_color birth_year gender homeworld\n                                   \n1 C-3PO 167 75 gold yellow 112 Tatooine \n2 R2-D2 96 32 white, bl~ red 33 Naboo    \n3 R5-D4 97 32 white, red red NA Tatooine \n4 IG-88 200 140 none metal red 15 none        \n5 BB8 NA NA none none black NA none        \n# ... with 4 more variables: species , films , vehicles ,\n# starships \n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Podemos ver que 5 linhas do conjunto de dados atendem a essa condi\u00e7\u00e3o, conforme indicado por <em>#A tibble: 5 x 13<\/em> .<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Exemplo 2: Filtrar linhas usando \u201cE\u201d<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Tamb\u00e9m podemos filtrar linhas onde a esp\u00e9cie \u00e9 Droid <strong>e<\/strong> a cor dos olhos \u00e9 vermelha:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>starwars %&gt;% filter(species == ' <span style=\"color: #008000;\">Droid<\/span> ' <span style=\"color: #993300;\">&amp;<\/span> eye_color == ' <span style=\"color: #008000;\">red<\/span> ')\n\n# A tibble: 3 x 13\n  name height mass hair_color skin_color eye_color birth_year gender homeworld\n                                   \n1 R2-D2 96 32 &lt;NA&gt; white, bl~ red 33 &lt;NA&gt; Naboo    \n2 R5-D4 97 32 &lt;NA&gt; white, red red NA &lt;NA&gt; Tatooine \n3 IG-88 200 140 none metal red 15 none &lt;NA&gt;      \n# ... with 4 more variables: species , films , vehicles ,\n# starships \n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Podemos ver que 3 linhas no conjunto de dados atendem a essa condi\u00e7\u00e3o.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Exemplo 3: Filtrar linhas usando \u201cOr\u201d<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Tamb\u00e9m podemos filtrar linhas onde a esp\u00e9cie \u00e9 Droid <strong>ou<\/strong> a cor dos olhos \u00e9 vermelha:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>starwars %&gt;% filter(species == ' <span style=\"color: #008000;\">Droid<\/span> ' <span style=\"color: #993300;\">|<\/span> eye_color == ' <span style=\"color: #008000;\">red<\/span> ')\n\n# A tibble: 7 x 13\n  name height mass hair_color skin_color eye_color birth_year gender homeworld\n                                   \n1 C-3PO 167 75 &lt;NA&gt; gold yellow 112 &lt;NA&gt; Tatooine \n2 R2-D2 96 32 &lt;NA&gt; white, bl~ red 33 &lt;NA&gt; Naboo    \n3 R5-D4 97 32 &lt;NA&gt; white, red red NA &lt;NA&gt; Tatooine \n4 IG-88 200 140 none metal red 15 none &lt;NA&gt;     \n5 Bossk 190 113 none green red 53 male Trandosha\n6 Nute~ 191 90 none mottled g~ red NA male Cato Nei~\n7 BB8 NA NA none none black NA none &lt;NA&gt;     \n# ... with 4 more variables: species , films , vehicles ,\n# starships  \n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Podemos ver que 7 linhas no conjunto de dados atendem a essa condi\u00e7\u00e3o.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Exemplo 4: Filtrar linhas com valores em uma lista<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Tamb\u00e9m podemos filtrar linhas onde a cor dos olhos aparece em uma lista de cores:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>starwars %&gt;% filter(eye_color <span style=\"color: #993300;\">%in%<\/span> c(' <span style=\"color: #008000;\">blue<\/span> ', ' <span style=\"color: #008000;\">yellow<\/span> ', ' <span style=\"color: #008000;\">red<\/span> '))\n\n# A tibble: 35 x 13\n   name height mass hair_color skin_color eye_color birth_year gender\n                               \n 1 Luke~ 172 77 blond fair blue 19 male  \n 2 C-3PO 167 75 &lt;NA&gt; gold yellow 112 &lt;NA&gt; \n 3 R2-D2 96 32 &lt;NA&gt; white, bl~ red 33 &lt;NA&gt;  \n 4 Dart~ 202 136 none white yellow 41.9 male  \n 5 Owen~ 178 120 brown, gr~ light blue 52 male  \n 6 Beru~ 165 75 brown light blue 47 female\n 7 R5-D4 97 32 &lt;NA&gt; white, red red NA &lt;NA&gt; \n 8 Anak~ 188 84 blond fair blue 41.9 male  \n 9 Wilh~ 180 NA auburn, g~ fair blue 64 male  \n10 Chew~ 228 112 brown unknown blue 200 male  \n# ... with 25 more rows, and 5 more variables: homeworld, species,\n# films, vehicles, starships  \n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Podemos ver que 35 linhas no conjunto de dados tinham olhos azuis, amarelos ou vermelhos.<\/span><\/p>\n<p> <strong><span style=\"color: #000000;\">Relacionado:<\/span><\/strong> <a href=\"https:\/\/statorials.org\/pt\/operador-in-em-r\/\" target=\"_blank\" rel=\"noopener noreferrer\">Como usar o operador% in% em R (com exemplos)<\/a><\/p>\n<h3> <strong><span style=\"color: #000000;\">Exemplo 5: Filtrar linhas usando menor ou maior que<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Tamb\u00e9m podemos filtrar linhas usando opera\u00e7\u00f5es menores ou maiores que em vari\u00e1veis num\u00e9ricas:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#find rows where height is greater than 250<\/span>\nstarwars %&gt;% filter(height <span style=\"color: #993300;\">&gt;<\/span> 250)\n\n# A tibble: 1 x 13\n  name height mass hair_color skin_color eye_color birth_year gender homeworld\n                                   \n1 Yara~ 264 NA none white yellow NA male Quermia  \n# ... with 4 more variables: species , films , vehicles ,\n# starships   \n\n<span style=\"color: #008080;\">#find rows where height is between 200 and 220<\/span>\nstarwars %&gt;% filter(height <span style=\"color: #993300;\">&gt;<\/span> 200 <span style=\"color: #993300;\">&amp;<\/span> height <span style=\"color: #993300;\">&lt;<\/span> 220)\n\n# A tibble: 5 x 13\n  name height mass hair_color skin_color eye_color birth_year gender homeworld\n                                   \n1 Dart~ 202 136 none white yellow 41.9 male Tatooine \n2 Rugo~ 206 NA none green orange NA male Naboo    \n3 Taun~ 213 NA none gray black NA female Kamino   \n4 Grie~ 216 159 none brown, wh~ green, y~ NA male Kalee    \n5 Tion~ 206 80 none gray black NA male Utapau   \n# ... with 4 more variables: species , films , vehicles ,\n# starships \n\n<span style=\"color: #008080;\">#find rows where height is above the average height\n<span style=\"color: #000000;\">starwars %&gt;% filter(height <span style=\"color: #993300;\">&gt;<\/span> <span style=\"color: #3366ff;\">mean<\/span> (height, na.rm = <span style=\"color: #008000;\">TRUE<\/span> ))\n\n# A tibble: 51 x 13\n   name height mass hair_color skin_color eye_color birth_year gender\n                               \n 1 Dart~ 202 136 none white yellow 41.9 male  \n 2 Owen~ 178 120 brown, gr~ light blue 52 male  \n 3 Bigg~ 183 84 black light brown 24 male  \n 4 Obi-~ 182 77 auburn, w~ fair blue-gray 57 male  \n 5 Anak~ 188 84 blond fair blue 41.9 male  \n 6 Wilh~ 180 NA auburn, g~ fair blue 64 male  \n 7 Chew~ 228 112 brown unknown blue 200 male  \n 8 Han ~ 180 80 brown fair brown 29 male  \n 9 Jabb~ 175 1358 &lt;NA&gt; green-tan~ orange 600 herma~\n10 Jek ~ 180 110 brown fair blue NA male  \n# ... with 41 more rows, and 5 more variables: homeworld, species,\n# films, vehicles, starships<\/span><\/span>\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><em>Voc\u00ea pode encontrar a documenta\u00e7\u00e3o completa da fun\u00e7\u00e3o <strong>filter()<\/strong> <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/filter.html\" target=\"_blank\" rel=\"noopener noreferrer\">aqui<\/a> .<\/em><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Freq\u00fcentemente, voc\u00ea pode estar interessado em um subconjunto de um quadro de dados baseado em certas condi\u00e7\u00f5es em R. Felizmente, isso \u00e9 f\u00e1cil de fazer usando a fun\u00e7\u00e3o filter() do pacote dplyr . library(dplyr) Este tutorial explica v\u00e1rios exemplos de como usar esse recurso na pr\u00e1tica usando o conjunto de dados dplyr integrado chamado starwars [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-943","post","type-post","status-publish","format-standard","hentry","category-guia"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Como filtrar linhas em R \u2013 Estatologia<\/title>\n<meta name=\"description\" content=\"Uma explica\u00e7\u00e3o simples sobre como filtrar dados em R usando a fun\u00e7\u00e3o filter() do pacote dplyr.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como filtrar linhas em R \u2013 Estatologia\" \/>\n<meta property=\"og:description\" content=\"Uma explica\u00e7\u00e3o simples sobre como filtrar dados em R usando a fun\u00e7\u00e3o filter() do pacote dplyr.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T05:49:15+00:00\" \/>\n<meta name=\"author\" content=\"Dr. benjamim anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr. benjamim anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tempo estimado de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/\",\"url\":\"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/\",\"name\":\"Como filtrar linhas em R \u2013 Estatologia\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-28T05:49:15+00:00\",\"dateModified\":\"2023-07-28T05:49:15+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Uma explica\u00e7\u00e3o simples sobre como filtrar dados em R usando a fun\u00e7\u00e3o filter() do pacote dplyr.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como filtrar linhas em r\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/pt\/#website\",\"url\":\"https:\/\/statorials.org\/pt\/\",\"name\":\"Statorials\",\"description\":\"O seu guia para a literacia estat\u00edstica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/pt\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pt-PT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\",\"name\":\"Dr. benjamim anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-PT\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. benjamim anderson\"},\"description\":\"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais\",\"sameAs\":[\"https:\/\/statorials.org\/pt\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Como filtrar linhas em R \u2013 Estatologia","description":"Uma explica\u00e7\u00e3o simples sobre como filtrar dados em R usando a fun\u00e7\u00e3o filter() do pacote dplyr.","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:\/\/statorials.org\/pt\/filtrar-linhas-r\/","og_locale":"pt_PT","og_type":"article","og_title":"Como filtrar linhas em R \u2013 Estatologia","og_description":"Uma explica\u00e7\u00e3o simples sobre como filtrar dados em R usando a fun\u00e7\u00e3o filter() do pacote dplyr.","og_url":"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/","og_site_name":"Statorials","article_published_time":"2023-07-28T05:49:15+00:00","author":"Dr. benjamim anderson","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"Dr. benjamim anderson","Tempo estimado de leitura":"5 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/","url":"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/","name":"Como filtrar linhas em R \u2013 Estatologia","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-28T05:49:15+00:00","dateModified":"2023-07-28T05:49:15+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Uma explica\u00e7\u00e3o simples sobre como filtrar dados em R usando a fun\u00e7\u00e3o filter() do pacote dplyr.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/filtrar-linhas-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/filtrar-linhas-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como filtrar linhas em r"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/pt\/#website","url":"https:\/\/statorials.org\/pt\/","name":"Statorials","description":"O seu guia para a literacia estat\u00edstica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/pt\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pt-PT"},{"@type":"Person","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666","name":"Dr. benjamim anderson","image":{"@type":"ImageObject","inLanguage":"pt-PT","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr. benjamim anderson"},"description":"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais","sameAs":["https:\/\/statorials.org\/pt"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/comments?post=943"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/943\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}