{"id":2893,"date":"2023-07-20T05:31:17","date_gmt":"2023-07-20T05:31:17","guid":{"rendered":"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/"},"modified":"2023-07-20T05:31:17","modified_gmt":"2023-07-20T05:31:17","slug":"data-del-filtro-dplyr","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/","title":{"rendered":"Come filtrare per data usando dplyr"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare i seguenti metodi per filtrare un frame di dati in base alle date in R utilizzando il pacchetto <a href=\"https:\/\/dplyr.tidyverse.org\/\" target=\"_blank\" rel=\"noopener\">dplyr<\/a> :<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: filtra le righe dopo la data<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df %&gt;% filter(date_column &gt; ' <span style=\"color: #ff0000;\">2022-01-01<\/span> ')\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: filtra le righe prima della data<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df %&gt;% filter(date_column &lt; ' <span style=\"color: #ff0000;\">2022-01-01<\/span> ') \n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 3: filtra le righe tra due date<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df %&gt;% filter(between(date_column, as. <span style=\"color: #3366ff;\">Date<\/span> (' <span style=\"color: #ff0000;\">2022-01-20<\/span> '), as. <span style=\"color: #3366ff;\">Date<\/span> (' <span style=\"color: #ff0000;\">2022-02-20<\/span> ')))\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">I seguenti esempi mostrano come utilizzare ciascun metodo nella pratica con il seguente frame di dati in R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#create data frame\n<\/span>df &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (day=seq( <span style=\"color: #3366ff;\">as.Date<\/span> (' <span style=\"color: #ff0000;\">2022-01-01<\/span> '), by = ' <span style=\"color: #ff0000;\">week<\/span> ', <span style=\"color: #3366ff;\">length.out<\/span> = <span style=\"color: #008000;\">10<\/span> ),\n                 sales=c(40, 35, 39, 44, 48, 51, 23, 29, 60, 65))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n          day sales\n1 2022-01-01 40\n2 2022-01-08 35\n3 2022-01-15 39\n4 2022-01-22 44\n5 2022-01-29 48\n6 2022-02-05 51\n7 2022-02-12 23\n8 2022-02-19 29\n9 2022-02-26 60\n10 2022-03-05 65\n<\/strong><\/span><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: filtrare le righe dopo la data<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare il seguente codice per filtrare le righe dal frame di dati che hanno una data successiva al 25\/01\/2022:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">library<\/span> (dplyr)<\/span>\n\n#filter for rows with date after 1\/25\/2022\n<\/span>df %&gt;% filter(day &gt; ' <span style=\"color: #ff0000;\">2022-01-25<\/span> ')\n\n         day sales\n1 2022-01-29 48\n2 2022-02-05 51\n3 2022-02-12 23\n4 2022-02-19 29\n5 2022-02-26 60\n6 2022-03-05 65\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Ciascuna delle righe nel frame di dati risultante ha una data successiva al 25\/01\/2022.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: filtrare le righe prima della data<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare il seguente codice per filtrare le righe dal frame di dati la cui data \u00e8 precedente al 25\/01\/2022:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\"><span style=\"color: #008000;\">library<\/span> <span style=\"color: #000000;\">(dplyr)<\/span>\n\n#filter for rows with date before 1\/25\/2022\n<\/span>df %&gt;% filter(day &lt; ' <span style=\"color: #ff0000;\">2022-01-25<\/span> ')\n\n         day sales\n1 2022-01-01 40\n2 2022-01-08 35\n3 2022-01-15 39\n4 2022-01-22 44\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Ciascuna delle righe nel frame di dati risultante ha una data precedente al 25\/01\/2022.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 3: filtrare le righe tra due date<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare il seguente codice per filtrare le righe dal frame di dati la cui data \u00e8 compresa tra il 20\/01\/2022 e il 20\/02\/2022:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\"><span style=\"color: #008000;\">library<\/span> <span style=\"color: #000000;\">(dplyr)<\/span>\n\n#filter for rows with dates between 1\/20\/2022 and 2\/20\/2022\n<\/span>df %&gt;% filter(between(date_column, as. <span style=\"color: #3366ff;\">Date<\/span> (' <span style=\"color: #ff0000;\">2022-01-20<\/span> '), as. <span style=\"color: #3366ff;\">Date<\/span> (' <span style=\"color: #ff0000;\">2022-02-20<\/span> '))) \n\n         day sales\n1 2022-01-22 44\n2 2022-01-29 48\n3 2022-02-05 51\n4 2022-02-12 23\n5 2022-02-19 29\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Ciascuna delle righe nel frame di dati risultante ha una data compresa tra il 20\/01\/2022 e il 20\/02\/2022.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota n. 1<\/strong> : se uno qualsiasi dei metodi sopra indicati non funziona, potrebbe essere necessario prima convertire le date con cui stai lavorando in un formato di data riconoscibile utilizzando la funzione <strong>as.Date(<\/strong> <strong>)<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota n.2<\/strong> : puoi trovare la documentazione completa della funzione <strong>filtro<\/strong> in dplyr <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/filter.html\" target=\"_blank\" rel=\"noopener\">qui<\/a> .<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre operazioni comuni in dplyr:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/righe-di-filtro-che-contengono-la-stringa-dplyr\/\" target=\"_blank\" rel=\"noopener\">Come filtrare le righe contenenti una determinata stringa utilizzando dplyr<\/a><br \/> Come filtrare in base a pi\u00f9 condizioni utilizzando dplyr<br \/> <a href=\"https:\/\/statorials.org\/it\/filtro-dplyr-non-inserito\/\" target=\"_blank\" rel=\"noopener\">Come utilizzare un filtro &#8220;non incluso&#8221; in dplyr<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare i seguenti metodi per filtrare un frame di dati in base alle date in R utilizzando il pacchetto dplyr : Metodo 1: filtra le righe dopo la data df %&gt;% filter(date_column &gt; &#8216; 2022-01-01 &#8216;) Metodo 2: filtra le righe prima della data df %&gt;% filter(date_column &lt; &#8216; 2022-01-01 &#8216;) Metodo 3: [&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 per data utilizzando dplyr - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come filtrare le righe in un frame di dati per data utilizzando dplyr, con esempi.\" \/>\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\/data-del-filtro-dplyr\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come filtrare per data utilizzando dplyr - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come filtrare le righe in un frame di dati per data utilizzando dplyr, con esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-20T05:31:17+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=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/\",\"url\":\"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/\",\"name\":\"Come filtrare per data utilizzando dplyr - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-20T05:31:17+00:00\",\"dateModified\":\"2023-07-20T05:31:17+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come filtrare le righe in un frame di dati per data utilizzando dplyr, con esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come filtrare per data usando dplyr\"}]},{\"@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 per data utilizzando dplyr - Statorials","description":"Questo tutorial spiega come filtrare le righe in un frame di dati per data utilizzando dplyr, con esempi.","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\/data-del-filtro-dplyr\/","og_locale":"it_IT","og_type":"article","og_title":"Come filtrare per data utilizzando dplyr - Statorials","og_description":"Questo tutorial spiega come filtrare le righe in un frame di dati per data utilizzando dplyr, con esempi.","og_url":"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/","og_site_name":"Statorials","article_published_time":"2023-07-20T05:31:17+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/","url":"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/","name":"Come filtrare per data utilizzando dplyr - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-20T05:31:17+00:00","dateModified":"2023-07-20T05:31:17+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come filtrare le righe in un frame di dati per data utilizzando dplyr, con esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/data-del-filtro-dplyr\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come filtrare per data usando dplyr"}]},{"@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\/2893"}],"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=2893"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2893\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}