{"id":4317,"date":"2023-07-12T01:49:33","date_gmt":"2023-07-12T01:49:33","guid":{"rendered":"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/"},"modified":"2023-07-12T01:49:33","modified_gmt":"2023-07-12T01:49:33","slug":"numero-di-parole-nel-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/","title":{"rendered":"Come contare le parole in una stringa in r (con esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Esistono tre metodi che puoi utilizzare per contare il numero di parole in una stringa in R:<\/span><\/p>\n<p> <strong><span style=\"color: #000000;\">Metodo 1: utilizzare Base R<\/span><\/strong><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>lengths(strsplit(my_string, ' '))\n<\/strong><\/pre>\n<p> <strong><span style=\"color: #000000;\">Metodo 2: utilizzare il pacchetto stringi<\/span><\/strong><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (stringi)\n\nstri_count_words(my_string)<\/strong><\/pre>\n<p> <strong><span style=\"color: #000000;\">Metodo 3: utilizzare il pacchetto stringr<\/span><\/strong><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (stringr)\n\nstr_count(my_string, ' <span style=\"color: #ff0000;\">\\\\w+<\/span> ')\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Ciascuno di questi metodi restituir\u00e0 un valore numerico che rappresenta il numero di parole nella stringa chiamata <strong>my_string<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come utilizzare nella pratica ciascuno di questi metodi.<\/span><\/p>\n<h2> <strong><span style=\"color: #000000;\">Esempio 1: contare le parole utilizzando la base R<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come contare il numero di parole in una stringa utilizzando le funzioni di base R <strong>lengths<\/strong> e <strong>strsplit<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create string\n<\/span>my_string &lt;- 'this is a string with seven words'\n\n<span style=\"color: #008080;\">#count number of words in string<\/span>\nlengths(strsplit(my_string, ' '))\n\n[1] 7\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Dal risultato possiamo vedere che ci sono sette parole nella stringa.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Correlati:<\/strong> <a href=\"https:\/\/statorials.org\/it\/funzione-strsplit-in-r\/\" target=\"_blank\" rel=\"noopener\">come utilizzare la funzione strsplit() in R per dividere gli elementi di una stringa<\/a><\/span><\/p>\n<h2> <strong><span style=\"color: #000000;\">Esempio 2:<\/span><\/strong> <strong><span style=\"color: #000000;\">contare le parole utilizzando il pacchetto stringi<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come contare il numero di parole in una stringa utilizzando la funzione <b>stri_count_words<\/b> dal pacchetto <strong>stringi<\/strong> in R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">library<\/span> (stringi)<\/span>\n\n#create string\n<\/span>my_string &lt;- 'this is a string with seven words'\n\n<span style=\"color: #008080;\">#count number of words in string<\/span>\nstri_count_words(my_string)\n\n[1] 7\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Dal risultato possiamo vedere che ci sono sette parole nella stringa.<\/span><\/p>\n<h2> <strong><span style=\"color: #000000;\">Esempio 3:<\/span><\/strong> <strong><span style=\"color: #000000;\">contare le parole utilizzando il pacchetto stringr<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come contare il numero di parole in una stringa utilizzando la funzione <b>str_count<\/b> dal pacchetto <strong>stringr<\/strong> in R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">library<\/span> (stringr)<\/span>\n\n#create string\n<\/span>my_string &lt;- 'this is a string with seven words'\n\n<span style=\"color: #008080;\">#count number of words in string<\/span>\nstr_count(my_string, ' <span style=\"color: #ff0000;\">\\\\w+<\/span> ')\n\n[1] 7\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Dal risultato possiamo vedere che ci sono sette parole nella stringa.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tieni presente che abbiamo utilizzato l&#8217;espressione regolare <strong>\\\\w+<\/strong> per abbinare caratteri non verbali con il segno <strong>+<\/strong> per indicarne uno o pi\u00f9 in una riga.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : in ciascuno di questi esempi abbiamo contato il numero di parole in una singola stringa, ma ciascun metodo funzioner\u00e0 anche con un vettore di stringhe.<\/span><\/p>\n<h2> <strong><span style=\"color: #000000;\">Risorse addizionali<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre attivit\u00e0 comuni in R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/r-cerca-il-carattere-nella-stringa\/\" target=\"_blank\" rel=\"noopener\">Come trovare la posizione del carattere in una stringa in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/r-rimuove-il-carattere-dalla-stringa\/\" target=\"_blank\" rel=\"noopener\">Come rimuovere caratteri da una stringa in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/r-seleziona-le-colonne-contenenti-la-stringa\/\" target=\"_blank\" rel=\"noopener\">Come selezionare colonne contenenti una stringa specifica in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Esistono tre metodi che puoi utilizzare per contare il numero di parole in una stringa in R: Metodo 1: utilizzare Base R lengths(strsplit(my_string, &#8216; &#8216;)) Metodo 2: utilizzare il pacchetto stringi library (stringi) stri_count_words(my_string) Metodo 3: utilizzare il pacchetto stringr library (stringr) str_count(my_string, &#8216; \\\\w+ &#8216;) Ciascuno di questi metodi restituir\u00e0 un valore numerico che [&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 contare le parole in una stringa in R (con esempi) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come contare il numero di parole in una stringa in R, con diversi 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\/numero-di-parole-nel-r\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come contare le parole in una stringa in R (con esempi) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come contare il numero di parole in una stringa in R, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-12T01:49:33+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\/numero-di-parole-nel-r\/\",\"url\":\"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/\",\"name\":\"Come contare le parole in una stringa in R (con esempi) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-12T01:49:33+00:00\",\"dateModified\":\"2023-07-12T01:49:33+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come contare il numero di parole in una stringa in R, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come contare le parole in una stringa in r (con esempi)\"}]},{\"@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 contare le parole in una stringa in R (con esempi) \u2013 Statorials","description":"Questo tutorial spiega come contare il numero di parole in una stringa in R, con diversi 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\/numero-di-parole-nel-r\/","og_locale":"it_IT","og_type":"article","og_title":"Come contare le parole in una stringa in R (con esempi) \u2013 Statorials","og_description":"Questo tutorial spiega come contare il numero di parole in una stringa in R, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/","og_site_name":"Statorials","article_published_time":"2023-07-12T01:49:33+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\/numero-di-parole-nel-r\/","url":"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/","name":"Come contare le parole in una stringa in R (con esempi) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-12T01:49:33+00:00","dateModified":"2023-07-12T01:49:33+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come contare il numero di parole in una stringa in R, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/numero-di-parole-nel-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come contare le parole in una stringa in r (con esempi)"}]},{"@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\/4317"}],"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=4317"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4317\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}