{"id":3331,"date":"2023-07-18T01:38:17","date_gmt":"2023-07-18T01:38:17","guid":{"rendered":"https:\/\/statorials.org\/it\/str_sub-in-r\/"},"modified":"2023-07-18T01:38:17","modified_gmt":"2023-07-18T01:38:17","slug":"str_sub-in-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/str_sub-in-r\/","title":{"rendered":"Come utilizzare str_sub in r (con esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">La funzione <strong>str_sub()<\/strong> del pacchetto <a href=\"https:\/\/stringr.tidyverse.org\/\" target=\"_blank\" rel=\"noopener\">stringr<\/a> in R pu\u00f2 essere utilizzata per estrarre o sostituire sottostringhe in una stringa.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questa funzione utilizza la seguente sintassi:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>str_sub(stringa, inizio, fine)<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Oro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>stringa:<\/strong> vettore di caratteri<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>inizio:<\/strong> Posizione del primo carattere<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>end:<\/strong> posizione dell&#8217;ultimo carattere<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Questo tutorial fornisce diversi esempi di come utilizzare praticamente questa funzione con il seguente frame di dati:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create data frame<\/span>\ndf &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (team=c('team_A', 'team_B', 'team_C', 'team_D'),\n                 conference=c('West', 'West', 'East', 'East'),\n                 dots=c(88, 97, 94, 104))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n    team conference points\n1 team_A West 88\n2 team_B West 97\n3 team_C East 94\n4 team_D East 104<\/strong><\/pre>\n<h2> <strong><span style=\"color: #000000;\">Esempio 1: estrarre una sottostringa da una stringa<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come estrarre la sottostringa che inizia alla posizione 5 e termina alla posizione 6 per ogni stringa nella colonna &#8220;team&#8221;:<\/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<\/span><span style=\"color: #008080;\">#extract characters in positions 5 through 6 of 'team' column\n<\/span>str_sub(string=df$team, start= <span style=\"color: #008000;\">5<\/span> , end= <span style=\"color: #008000;\">6<\/span> )\n\n[1] \"_A\" \"_B\" \"_C\" \"_D\"\n<\/strong><\/pre>\n<h2> <strong><span style=\"color: #000000;\">Esempio 2: estrarre la sottostringa in una posizione specifica<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come estrarre ogni carattere fino alla posizione 4 per ogni stringa nella colonna &#8220;team&#8221;:<\/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<\/span><span style=\"color: #008080;\">#extract all characters up to position 4 in 'team' column\n<\/span>str_sub(string=df$team, end= <span style=\"color: #008000;\">4<\/span> )\n\n[1] \"team\" \"team\" \"team\" \"team\"<\/strong><\/pre>\n<h2> <strong><span style=\"color: #000000;\">Esempio 3: estrae la sottostringa da una posizione specifica<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come estrarre ogni carattere dopo la posizione 3 per ogni stringa nella colonna &#8220;team&#8221;:<\/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<\/span><span style=\"color: #008080;\">#extract all characters after position 2 in 'team' column\n<\/span>str_sub(string=df$team, start= <span style=\"color: #008000;\">3<\/span> )\n\n[1] \"am_A\" \"am_B\" \"am_C\" \"am_D\"\n<\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 4: sostituire una sottostringa in una stringa<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come sostituire la sottostringa che inizia alla posizione 1 e termina alla posizione 5 per ogni stringa nella colonna &#8220;team&#8221;:<\/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<\/span><span style=\"color: #008080;\">#replace all characters between position 1 and 5 in 'team' column\n<\/span>str_sub(string=df$team, start= <span style=\"color: #008000;\">1<\/span> , end= <span style=\"color: #008000;\">5<\/span> ) &lt;- 'TEAM'\n\n<span style=\"color: #008080;\">#view updated data frame<\/span>\ndf\n\n   team conference points\n1 TEAMA West 88\n2 TEAMB West 97\n3 TEAMC East 94\n4 TEAMD East 104<\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/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\/str_sostituisci-nella-r\/\" target=\"_blank\" rel=\"noopener\">Come utilizzare str_replace in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/str_diviso-in-r\/\" target=\"_blank\" rel=\"noopener\">Come utilizzare str_split in R<\/a><br \/> Come utilizzare str_detect in R<\/p>\n","protected":false},"excerpt":{"rendered":"<p>La funzione str_sub() del pacchetto stringr in R pu\u00f2 essere utilizzata per estrarre o sostituire sottostringhe in una stringa. Questa funzione utilizza la seguente sintassi: str_sub(stringa, inizio, fine) Oro: stringa: vettore di caratteri inizio: Posizione del primo carattere end: posizione dell&#8217;ultimo carattere Questo tutorial fornisce diversi esempi di come utilizzare praticamente questa funzione con il [&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 utilizzare str_sub in R (con esempi) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come utilizzare la funzione str_sub 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\/str_sub-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come utilizzare str_sub in R (con esempi) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come utilizzare la funzione str_sub in R, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/str_sub-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-18T01:38: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\/str_sub-in-r\/\",\"url\":\"https:\/\/statorials.org\/it\/str_sub-in-r\/\",\"name\":\"Come utilizzare str_sub in R (con esempi) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-18T01:38:17+00:00\",\"dateModified\":\"2023-07-18T01:38:17+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come utilizzare la funzione str_sub in R, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/str_sub-in-r\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/str_sub-in-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/str_sub-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come utilizzare str_sub 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 utilizzare str_sub in R (con esempi) \u2013 Statorials","description":"Questo tutorial spiega come utilizzare la funzione str_sub 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\/str_sub-in-r\/","og_locale":"it_IT","og_type":"article","og_title":"Come utilizzare str_sub in R (con esempi) \u2013 Statorials","og_description":"Questo tutorial spiega come utilizzare la funzione str_sub in R, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/str_sub-in-r\/","og_site_name":"Statorials","article_published_time":"2023-07-18T01:38: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\/str_sub-in-r\/","url":"https:\/\/statorials.org\/it\/str_sub-in-r\/","name":"Come utilizzare str_sub in R (con esempi) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-18T01:38:17+00:00","dateModified":"2023-07-18T01:38:17+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come utilizzare la funzione str_sub in R, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/str_sub-in-r\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/str_sub-in-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/str_sub-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come utilizzare str_sub 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\/3331"}],"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=3331"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3331\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}