{"id":2972,"date":"2023-07-19T20:31:04","date_gmt":"2023-07-19T20:31:04","guid":{"rendered":"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/"},"modified":"2023-07-19T20:31:04","modified_gmt":"2023-07-19T20:31:04","slug":"funzione-sottostringa-in-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/","title":{"rendered":"Come utilizzare la funzione sottostringa in r (4 esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">La funzione <strong>substring()<\/strong> in R pu\u00f2 essere utilizzata per estrarre una sottostringa in un vettore di caratteri.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questa funzione utilizza la seguente sintassi:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong>substring(text, first, last)<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Oro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>testo:<\/strong> nome del vettore del carattere<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>first:<\/strong> il primo elemento da estrarre<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>last:<\/strong> l&#8217;ultimo elemento da estrarre<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Tieni inoltre presente che la funzione <strong>substr()<\/strong> fa esattamente la stessa cosa, ma con nomi di argomenti leggermente diversi:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong>substr(text, first, last)<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Oro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>x:<\/strong> nome del vettore di caratteri<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>start:<\/strong> il primo elemento da estrarre<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>stop:<\/strong> l&#8217;ultimo elemento da estrarre<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Gli esempi in questo tutorial mostrano come utilizzare in pratica la funzione <strong>substring()<\/strong> 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> (team=c('Mavericks', 'Hornets', 'Rockets', 'Grizzlies'))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n       team\n1 Mavericks\n2 Hornets\n3 Rockets\n4 Grizzlies\n<\/strong><\/span><\/pre>\n<h3> <strong><span style=\"color: #000000;\">Esempio 1: estrarre i caratteri tra determinate posizioni<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <strong>substring()<\/strong> per estrarre i caratteri tra le posizioni 2 e 5 della colonna &#8220;team&#8221;:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#create new column that contains characters between positions 2 and 5\n<\/span>df$between2_5 &lt;- substring(df$team, first= <span style=\"color: #008000;\">2<\/span> , last= <span style=\"color: #008000;\">5<\/span> )\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n       team between2_5\n1 Mavericks aver\n2 Hornets adorns\n3 Rockets ocke\n4 Rizz Grizzlies<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Da notare che la nuova colonna contiene i caratteri compresi tra le posizioni 2 e 5 della colonna &#8220;team&#8221;.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Esempio 2: estrarre i primi N caratteri<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <strong>substring()<\/strong> per estrarre i primi 3 caratteri dalla colonna &#8220;team&#8221;:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#create new column that contains first 3 characters\n<\/span>df$first3 &lt;- substring(df$team, first= <span style=\"color: #008000;\">1<\/span> , last= <span style=\"color: #008000;\">3<\/span> )\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n       team first3\n1 Mavericks Mavs\n2 Hornets Hor\n3 Rockets Roc\n4 Grizzlies Gray<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che la nuova colonna contiene i primi tre caratteri della colonna &#8220;team&#8221;.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Esempio 3: estrarre gli ultimi N caratteri<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <strong>substring()<\/strong> per estrarre gli ultimi 3 caratteri dalla colonna &#8220;team&#8221;:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#create new column that contains last 3 characters\n<\/span>df$last3 &lt;- substring(df$team, <span style=\"color: #3366ff;\">nchar<\/span> (df$team)- <span style=\"color: #008000;\">3<\/span> +1, <span style=\"color: #3366ff;\">nchar<\/span> (df$team))\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n       team last3\n1 Mavericks cks\n2 Hornets ets\n3 Rockets ets\n4 Grizzlies ies<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che la nuova colonna contiene gli ultimi tre caratteri della colonna &#8220;team&#8221;.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Esempio 4: sostituire una sottostringa<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <strong>substring()<\/strong> per sostituire i primi 3 caratteri dei valori nella colonna &#8220;team&#8221; con 3 asterischi:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#replace first 3 characters with asterisks in team column\n<\/span>substring(df$team, first= <span style=\"color: #008000;\">1<\/span> , last= <span style=\"color: #008000;\">3<\/span> ) &lt;- \" <span style=\"color: #ff0000;\">***<\/span> \"\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n       team\n1 ***ericks\n2 ***net\n3 ***kets\n4 ***zzlies<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che i primi tre caratteri del nome di ciascuna squadra sono stati sostituiti con asterischi.<\/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 con le stringhe 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 \/> Come eseguire la corrispondenza parziale delle stringhe in R<br \/> <a href=\"https:\/\/statorials.org\/it\/convertire-le-stringhe-in-date-in-r\/\" target=\"_blank\" rel=\"noopener\">Come convertire le stringhe in date in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/carattere-numerico-in-r\/\" target=\"_blank\" rel=\"noopener\">Come convertire un carattere in numerico in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>La funzione substring() in R pu\u00f2 essere utilizzata per estrarre una sottostringa in un vettore di caratteri. Questa funzione utilizza la seguente sintassi: substring(text, first, last) Oro: testo: nome del vettore del carattere first: il primo elemento da estrarre last: l&#8217;ultimo elemento da estrarre Tieni inoltre presente che la funzione substr() fa esattamente la stessa [&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 la funzione sottostringa in R (4 esempi) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come utilizzare la funzione substring() 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\/funzione-sottostringa-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 la funzione sottostringa in R (4 esempi) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come utilizzare la funzione substring() in R, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-19T20:31:04+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\/funzione-sottostringa-in-r\/\",\"url\":\"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/\",\"name\":\"Come utilizzare la funzione sottostringa in R (4 esempi) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-19T20:31:04+00:00\",\"dateModified\":\"2023-07-19T20:31:04+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come utilizzare la funzione substring() in R, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come utilizzare la funzione sottostringa in r (4 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 la funzione sottostringa in R (4 esempi) \u2013 Statorials","description":"Questo tutorial spiega come utilizzare la funzione substring() 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\/funzione-sottostringa-in-r\/","og_locale":"it_IT","og_type":"article","og_title":"Come utilizzare la funzione sottostringa in R (4 esempi) \u2013 Statorials","og_description":"Questo tutorial spiega come utilizzare la funzione substring() in R, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/","og_site_name":"Statorials","article_published_time":"2023-07-19T20:31:04+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\/funzione-sottostringa-in-r\/","url":"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/","name":"Come utilizzare la funzione sottostringa in R (4 esempi) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-19T20:31:04+00:00","dateModified":"2023-07-19T20:31:04+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come utilizzare la funzione substring() in R, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/funzione-sottostringa-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come utilizzare la funzione sottostringa in r (4 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\/2972"}],"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=2972"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2972\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}