{"id":4292,"date":"2023-07-12T06:02:14","date_gmt":"2023-07-12T06:02:14","guid":{"rendered":"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/"},"modified":"2023-07-12T06:02:14","modified_gmt":"2023-07-12T06:02:14","slug":"r-stringa-secondaria-finale","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/","title":{"rendered":"R: estrae la sottostringa dalla fine della stringa"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare i seguenti metodi per estrarre una sottostringa in R a partire dalla fine della stringa:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: utilizzare Base R<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define function to extract n characters starting from end\n<\/span>substr_end &lt;- <span style=\"color: #008000;\">function<\/span> (x, n){\n  substr(x, nchar(x)-n+ <span style=\"color: #008000;\">1<\/span> , nchar(x))\n}\n\n<span style=\"color: #008080;\">#extract 3 characters starting from end<\/span>\nsubstr_end(my_string, <span style=\"color: #008000;\">3<\/span> )\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: utilizzare il pacchetto stringr<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (stringr)\n\n<span style=\"color: #008080;\">#extract 3 characters starting from end<\/span> \nstr_sub(my_string, start = - <span style=\"color: #008000;\">3<\/span> )\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questi due esempi estraggono gli ultimi tre caratteri dalla stringa chiamata <strong>my_string<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">I seguenti esempi mostrano come utilizzare ciascun metodo nella pratica con il seguente frame di dati:<\/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', 'Lakers', 'Hawks', 'Nets', 'Warriors'),\n                 dots=c(100, 143, 129, 113, 123))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n       team points\n1 Mavericks 100\n2 Lakers 143\n3 Hawks 129\n4 Nets 113\n5 Warriors 123\n<\/strong><\/span><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 1: estrai la sottostringa dall&#8217;estremit\u00e0 utilizzando Base R<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come definire una funzione personalizzata in base R e quindi utilizzare la funzione per estrarre gli ultimi tre caratteri da ciascuna stringa nella colonna del <strong>team<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define function to extract n characters starting from end\n<\/span>substr_end &lt;- <span style=\"color: #008000;\">function<\/span> (x, n){\n  substr(x, nchar(x)-n+ <span style=\"color: #008000;\">1<\/span> , nchar(x))\n}\n\n<span style=\"color: #008080;\">#create new column that extracts last 3 characters from team column<\/span>\ndf$team_last3 &lt;- substr_end(my_string, <span style=\"color: #008000;\">3<\/span> )\n\n<span style=\"color: #008080;\">#view updated data frame<\/span>\ndf\n\n       team points team_last3\n1 Mavericks 100 cks\n2 Lakers 143ers\n3 Hawks 129 wks\n4 Nets 113 ets\n5 Warriors 123 gold\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Si noti che la nuova colonna chiamata <b>team_last3<\/b> contiene gli ultimi tre caratteri di ciascuna stringa nella colonna <strong>team<\/strong> del data frame.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 2: estrarre la sottostringa dalla fine utilizzando il pacchetto stringr<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <strong>str_sub()<\/strong> dal pacchetto <strong>stringr<\/strong> in R per estrarre gli ultimi tre caratteri da ciascuna stringa nella colonna <strong>team<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #000000;\"><span style=\"color: #008000;\">library<\/span> (stringr)\n<\/span>\n<span style=\"color: #008080;\">#create new column that extracts last 3 characters from team column<\/span>\ndf$team_last3 &lt;- str_sub(df$team, start = - <span style=\"color: #008000;\">3<\/span> )\n\n<span style=\"color: #008080;\">#view updated data frame<\/span>\ndf\n\n       team points team_last3\n1 Mavericks 100 cks\n2 Lakers 143ers\n3 Hawks 129 wks\n4 Nets 113 ets\n5 Warriors 123 gold\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Si noti che la nuova colonna chiamata <b>team_last3<\/b> contiene gli ultimi tre caratteri di ciascuna stringa nella colonna <strong>team<\/strong> del data frame.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ci\u00f2 corrisponde ai risultati del metodo precedente utilizzando la base R.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Correlati<\/strong> : <a href=\"https:\/\/statorials.org\/it\/str_sub-in-r\/\" target=\"_blank\" rel=\"noopener\">un&#8217;introduzione alla funzione str_sub in R<\/a><\/span><\/p>\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\/r-seleziona-le-colonne-contenenti-la-stringa\/\" target=\"_blank\" rel=\"noopener\">Come selezionare colonne contenenti una stringa specifica 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-cerca-il-carattere-nella-stringa\/\" target=\"_blank\" rel=\"noopener\">Come trovare la posizione del carattere in una stringa in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare i seguenti metodi per estrarre una sottostringa in R a partire dalla fine della stringa: Metodo 1: utilizzare Base R #define function to extract n characters starting from end substr_end &lt;- function (x, n){ substr(x, nchar(x)-n+ 1 , nchar(x)) } #extract 3 characters starting from end substr_end(my_string, 3 ) Metodo 2: utilizzare [&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>A: Estrai la sottostringa dalla fine della stringa - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come estrarre una sottostringa in R a partire dalla fine di una stringa, 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\/r-stringa-secondaria-finale\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A: Estrai la sottostringa dalla fine della stringa - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come estrarre una sottostringa in R a partire dalla fine di una stringa, con esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-12T06:02:14+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\/r-stringa-secondaria-finale\/\",\"url\":\"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/\",\"name\":\"A: Estrai la sottostringa dalla fine della stringa - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-12T06:02:14+00:00\",\"dateModified\":\"2023-07-12T06:02:14+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come estrarre una sottostringa in R a partire dalla fine di una stringa, con esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"R: estrae la sottostringa dalla fine della stringa\"}]},{\"@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":"A: Estrai la sottostringa dalla fine della stringa - Statorials","description":"Questo tutorial spiega come estrarre una sottostringa in R a partire dalla fine di una stringa, 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\/r-stringa-secondaria-finale\/","og_locale":"it_IT","og_type":"article","og_title":"A: Estrai la sottostringa dalla fine della stringa - Statorials","og_description":"Questo tutorial spiega come estrarre una sottostringa in R a partire dalla fine di una stringa, con esempi.","og_url":"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/","og_site_name":"Statorials","article_published_time":"2023-07-12T06:02:14+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\/r-stringa-secondaria-finale\/","url":"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/","name":"A: Estrai la sottostringa dalla fine della stringa - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-12T06:02:14+00:00","dateModified":"2023-07-12T06:02:14+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come estrarre una sottostringa in R a partire dalla fine di una stringa, con esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/r-stringa-secondaria-finale\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"R: estrae la sottostringa dalla fine della stringa"}]},{"@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\/4292"}],"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=4292"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4292\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}