{"id":944,"date":"2023-07-28T05:49:15","date_gmt":"2023-07-28T05:49:15","guid":{"rendered":"https:\/\/statorials.org\/it\/filtro-righe-r\/"},"modified":"2023-07-28T05:49:15","modified_gmt":"2023-07-28T05:49:15","slug":"filtro-righe-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/filtro-righe-r\/","title":{"rendered":"Come filtrare le righe in r"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Spesso potresti essere interessato a un sottoinsieme di un frame di dati basato su determinate condizioni in R. Fortunatamente, questo \u00e8 facile da fare utilizzando la funzione <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/filter.html\" target=\"_blank\" rel=\"noopener noreferrer\">filter()<\/a> del pacchetto <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;\">Questo tutorial spiega diversi esempi di come utilizzare praticamente questa funzionalit\u00e0 utilizzando il set di dati dplyr integrato chiamato <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;\">Esempio 1: filtrare le righe uguali a un determinato valore<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come filtrare il set di dati per le righe in cui la variabile &#8220;specie&#8221; \u00e8 uguale 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;\">Possiamo vedere che 5 righe del set di dati soddisfano questa condizione, come indicato da <em>#A tibble: 5 x 13<\/em> .<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Esempio 2: filtrare le righe utilizzando &#8220;E&#8221;<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo anche filtrare le righe in cui la specie \u00e8 Droid <strong>e<\/strong> il colore degli occhi \u00e8 rosso:<\/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;\">Possiamo vedere che 3 righe nel set di dati soddisfano questa condizione.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Esempio 3: filtrare le righe utilizzando &#8220;Or&#8221;<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo anche filtrare le righe in cui la specie \u00e8 Droid <strong>o<\/strong> il colore degli occhi \u00e8 rosso:<\/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;\">Possiamo vedere che 7 righe nel set di dati soddisfano questa condizione.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Esempio 4: filtrare le righe con valori in un elenco<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo anche filtrare le righe in cui appare il colore degli occhi in un elenco di colori:<\/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;\">Possiamo vedere che 35 righe nel set di dati avevano il colore degli occhi blu, giallo o rosso.<\/span><\/p>\n<p> <strong><span style=\"color: #000000;\">Correlato:<\/span><\/strong> <a href=\"https:\/\/statorials.org\/it\/nelloperatore-in-r\/\" target=\"_blank\" rel=\"noopener noreferrer\">Come utilizzare l&#8217;operatore %in% in R (con esempi)<\/a><\/p>\n<h3> <strong><span style=\"color: #000000;\">Esempio 5: filtrare le righe utilizzando minore di o maggiore di<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo anche filtrare le righe utilizzando operazioni minore o maggiore di su variabili numeriche:<\/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>Puoi trovare la documentazione completa per la funzione <strong>filter()<\/strong> <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/filter.html\" target=\"_blank\" rel=\"noopener noreferrer\">qui<\/a> .<\/em><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spesso potresti essere interessato a un sottoinsieme di un frame di dati basato su determinate condizioni in R. Fortunatamente, questo \u00e8 facile da fare utilizzando la funzione filter() del pacchetto dplyr . library(dplyr) Questo tutorial spiega diversi esempi di come utilizzare praticamente questa funzionalit\u00e0 utilizzando il set di dati dplyr integrato chiamato starwars : #view [&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":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come filtrare le righe in R \u2013 Stology<\/title>\n<meta name=\"description\" content=\"Una semplice spiegazione su come filtrare i dati in R utilizzando la funzione filter() dal pacchetto 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\/it\/filtro-righe-r\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come filtrare le righe in R \u2013 Stology\" \/>\n<meta property=\"og:description\" content=\"Una semplice spiegazione su come filtrare i dati in R utilizzando la funzione filter() dal pacchetto dplyr.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/filtro-righe-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=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/filtro-righe-r\/\",\"url\":\"https:\/\/statorials.org\/it\/filtro-righe-r\/\",\"name\":\"Come filtrare le righe in R \u2013 Stology\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-28T05:49:15+00:00\",\"dateModified\":\"2023-07-28T05:49:15+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Una semplice spiegazione su come filtrare i dati in R utilizzando la funzione filter() dal pacchetto dplyr.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/filtro-righe-r\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/filtro-righe-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/filtro-righe-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come filtrare le righe in r\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come filtrare le righe in R \u2013 Stology","description":"Una semplice spiegazione su come filtrare i dati in R utilizzando la funzione filter() dal pacchetto 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\/it\/filtro-righe-r\/","og_locale":"it_IT","og_type":"article","og_title":"Come filtrare le righe in R \u2013 Stology","og_description":"Una semplice spiegazione su come filtrare i dati in R utilizzando la funzione filter() dal pacchetto dplyr.","og_url":"https:\/\/statorials.org\/it\/filtro-righe-r\/","og_site_name":"Statorials","article_published_time":"2023-07-28T05:49:15+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"5 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/filtro-righe-r\/","url":"https:\/\/statorials.org\/it\/filtro-righe-r\/","name":"Come filtrare le righe in R \u2013 Stology","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-28T05:49:15+00:00","dateModified":"2023-07-28T05:49:15+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Una semplice spiegazione su come filtrare i dati in R utilizzando la funzione filter() dal pacchetto dplyr.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/filtro-righe-r\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/filtro-righe-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/filtro-righe-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come filtrare le righe in r"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/944"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=944"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/944\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}