{"id":955,"date":"2023-07-28T04:57:15","date_gmt":"2023-07-28T04:57:15","guid":{"rendered":"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/"},"modified":"2023-07-28T04:57:15","modified_gmt":"2023-07-28T04:57:15","slug":"grep-contro-grepl-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/","title":{"rendered":"Confronto tra grep() e grepl() in r: qual \u00e8 la differenza?"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Due funzioni che le persone spesso confondono in R sono <strong>grep()<\/strong> e <strong>grepl()<\/strong> . Entrambe le funzioni ti consentono di vedere se esiste un determinato modello in una stringa, ma restituiscono risultati diversi:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>grepl()<\/strong> restituisce TRUE quando esiste un modello in una stringa.<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>grep()<\/strong> restituisce un vettore di indici di stringa contenente il modello.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente illustra questa differenza:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create a vector of data<\/span>\ndata &lt;- c('P Guard', 'S Guard', 'S Forward', 'P Forward', 'Center')\n\n<span style=\"color: #3366ff;\">grep<\/span> ('Guard', data)\n[1] 1 2\n\n<span style=\"color: #3366ff;\">grepl<\/span> ('Guard', data) \n[1] TRUE TRUE FALSE FALSE FALSE<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano quando potresti voler utilizzare una di queste funzioni rispetto all&#8217;altra.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Quando usare grepl()<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\"><strong>1. Filtra le righe contenenti una determinata stringa<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Uno degli usi pi\u00f9 comuni di <strong>grepl()<\/strong> \u00e8 filtrare le righe in un frame di dati contenente una determinata stringa:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\">library(dplyr)<\/span>\n\n#create data frame<\/span>\ndf &lt;- data.frame(player = c('P Guard', 'S Guard', 'S Forward', 'P Forward', 'Center'),\n                 points = c(12, 15, 19, 22, 32),\n                 rebounds = c(5, 7, 7, 12, 11))\n\n<span style=\"color: #008080;\">#filter rows that contain the string 'Guard' in the player column\n<\/span><span style=\"color: #000000;\">df %&gt;% filter( <span style=\"color: #3366ff;\">grepl<\/span> ('Guard', player))\n\n   player points rebounds\n1 P Guard 12 5\n2 S Guard 15 7<\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Correlati:<\/strong> <a href=\"https:\/\/statorials.org\/it\/righe-di-filtro-che-contengono-la-stringa-dplyr\/\">come filtrare le righe contenenti una determinata stringa utilizzando dplyr<\/a><\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Quando usare grep()<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\"><strong>1. Seleziona le colonne che contengono una determinata stringa<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Puoi usare <strong>grep()<\/strong> per selezionare le colonne in un frame di dati contenente una determinata stringa:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\">library(dplyr)<\/span>\n\n#create data frame<\/span>\ndf &lt;- data.frame(player = c('P Guard', 'S Guard', 'S Forward', 'P Forward', 'Center'),\n                 points = c(12, 15, 19, 22, 32),\n                 rebounds = c(5, 7, 7, 12, 11))\n\n<span style=\"color: #008080;\">#select columns that contain the string 'p' in their name\n<\/span><span style=\"color: #000000;\">df %&gt;% select( <span style=\"color: #3366ff;\">grep<\/span> ('p', colnames(df)))\n\n     player points\n1 P Guard 12\n2 S Guard 15\n3S Forward 19\n4P Forward 22\n5 Center 32<\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>2. Contare il numero di righe contenenti una determinata stringa<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Puoi usare <strong>grep()<\/strong> per contare il numero di righe in un frame di dati contenente una determinata stringa:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create data frame<\/span>\ndf &lt;- data.frame(player = c('P Guard', 'S Guard', 'S Forward', 'P Forward', 'Center'),\n                 points = c(12, 15, 19, 22, 32),\n                 rebounds = c(5, 7, 7, 12, 11))\n\n<span style=\"color: #008080;\">#count how many rows contain the string 'Guard' in the player column\n<\/span><span style=\"color: #000000;\">length( <span style=\"color: #3366ff;\">grep<\/span> ('Guard', df$player))\n\n[1] 2<\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><em>Puoi trovare altri tutorial su R qui .<\/em><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Due funzioni che le persone spesso confondono in R sono grep() e grepl() . Entrambe le funzioni ti consentono di vedere se esiste un determinato modello in una stringa, ma restituiscono risultati diversi: grepl() restituisce TRUE quando esiste un modello in una stringa. grep() restituisce un vettore di indici di stringa contenente il modello. L&#8217;esempio [&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>Confronto tra grep() e grepl() in R: qual \u00e8 la differenza?<\/title>\n<meta name=\"description\" content=\"Una semplice spiegazione della differenza tra le funzioni grep() e grepl() in R.\" \/>\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\/grep-contro-grepl-r\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Confronto tra grep() e grepl() in R: qual \u00e8 la differenza?\" \/>\n<meta property=\"og:description\" content=\"Una semplice spiegazione della differenza tra le funzioni grep() e grepl() in R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T04:57: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=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/\",\"url\":\"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/\",\"name\":\"Confronto tra grep() e grepl() in R: qual \u00e8 la differenza?\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-28T04:57:15+00:00\",\"dateModified\":\"2023-07-28T04:57:15+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Una semplice spiegazione della differenza tra le funzioni grep() e grepl() in R.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Confronto tra grep() e grepl() in r: qual \u00e8 la differenza?\"}]},{\"@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":"Confronto tra grep() e grepl() in R: qual \u00e8 la differenza?","description":"Una semplice spiegazione della differenza tra le funzioni grep() e grepl() in R.","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\/grep-contro-grepl-r\/","og_locale":"it_IT","og_type":"article","og_title":"Confronto tra grep() e grepl() in R: qual \u00e8 la differenza?","og_description":"Una semplice spiegazione della differenza tra le funzioni grep() e grepl() in R.","og_url":"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/","og_site_name":"Statorials","article_published_time":"2023-07-28T04:57:15+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\/grep-contro-grepl-r\/","url":"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/","name":"Confronto tra grep() e grepl() in R: qual \u00e8 la differenza?","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-28T04:57:15+00:00","dateModified":"2023-07-28T04:57:15+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Una semplice spiegazione della differenza tra le funzioni grep() e grepl() in R.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/grep-contro-grepl-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/grep-contro-grepl-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Confronto tra grep() e grepl() in r: qual \u00e8 la differenza?"}]},{"@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\/955"}],"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=955"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/955\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}