{"id":3878,"date":"2023-07-15T00:45:54","date_gmt":"2023-07-15T00:45:54","guid":{"rendered":"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/"},"modified":"2023-07-15T00:45:54","modified_gmt":"2023-07-15T00:45:54","slug":"r-aggiungi-un-nuovo-livello-di-fattore","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/","title":{"rendered":"Come aggiungere un nuovo livello al fattore r (con esempio)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la seguente sintassi di base per aggiungere un nuovo livello a una variabile fattore in R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong>levels(df$my_factor) &lt;- c(levels(df$my_factor), ' <span style=\"color: #ff0000;\">new_level<\/span> ')<\/strong><\/span><b>\n<\/b><\/pre>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come utilizzare questa sintassi nella pratica.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio: aggiungere un nuovo livello al fattore in R<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere il seguente frame di dati in R che mostra il numero di vendite effettuate in diverse regioni per un negozio al dettaglio:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><b><span style=\"color: #008080;\">#create data frame\n<\/span>df &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (region=factor(c('A', 'B', NA, 'D', NA, 'F')),\n                 sales=c(12, 18, 21, 14, 34, 40))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n  regional sales\n1 to 12\n2 B 18\n3 &lt;NA&gt; 21\n4 D 14\n5 &lt;NA&gt; 34\n6 F 40\n<\/b><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Si noti che la variabile <strong>regione<\/strong> \u00e8 un fattore.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Per visualizzare i livelli di questo fattore, possiamo utilizzare la <strong>funzione Levels()<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><b><span style=\"color: #008080;\">#view factor levels for region\n<span style=\"color: #000000;\">levels(df$region)\n\n[1] \u201cA\u201d \u201cB\u201d \u201cD\u201d \u201cF\u201d\n<\/span><\/span><\/b><\/span><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Possiamo utilizzare la seguente sintassi per aggiungere un nuovo livello di fattore chiamato &#8220;nessuna regione&#8221;:<\/span><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#add factor level called 'no region'\n<\/span>levels(df$region) &lt;- c(levels(df$region), ' <span style=\"color: #ff0000;\">no region<\/span> ')\n\n<span style=\"color: #008080;\">#convert each NA to 'no region'\n<\/span>df$region[is. <span style=\"color: #3366ff;\">na<\/span> (df$region)] &lt;- ' <span style=\"color: #ff0000;\">no region<\/span> '\n\n<span style=\"color: #008080;\">#view factor levels for region\n<\/span>levels(df$region)\n\n[1] \u201cA\u201d \u201cB\u201d \u201cD\u201d \u201cF\u201d \u201cno region\u201d\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Come livello di fattore \u00e8 stato aggiunto il nuovo livello denominato &#8220;nessuna regione&#8221;.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se lo desideriamo, possiamo utilizzare la funzione <strong>table()<\/strong> per contare le occorrenze di ciascun livello di fattore:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#view occurrences of each factor level\n<\/span>table(df$region)\n\nABDF no region \n1 1 1 1 2 \n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Dal risultato, possiamo vedere che il nuovo livello di fattore chiamato &#8220;nessuna regione&#8221; appare due volte nella colonna <strong>regione<\/strong> del frame dati.<\/span><\/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\/fattore-numericamente-in-r\/\" target=\"_blank\" rel=\"noopener\">Come convertire un fattore in numerico in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/convertire-il-fattore-carattere-in-r\/\" target=\"_blank\" rel=\"noopener\">Come convertire un fattore in un carattere in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/riorganizzare-i-livelli-dei-fattori-in-r\/\" target=\"_blank\" rel=\"noopener\">Come riorganizzare i livelli dei fattori in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la seguente sintassi di base per aggiungere un nuovo livello a una variabile fattore in R: levels(df$my_factor) &lt;- c(levels(df$my_factor), &#8216; new_level &#8216;) L&#8217;esempio seguente mostra come utilizzare questa sintassi nella pratica. Esempio: aggiungere un nuovo livello al fattore in R Supponiamo di avere il seguente frame di dati in R che mostra [&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 aggiungere un nuovo livello al fattore R (con esempio) - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come aggiungere un nuovo livello ad un fattore in R, con un esempio.\" \/>\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-aggiungi-un-nuovo-livello-di-fattore\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come aggiungere un nuovo livello al fattore R (con esempio) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come aggiungere un nuovo livello ad un fattore in R, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-15T00:45:54+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-aggiungi-un-nuovo-livello-di-fattore\/\",\"url\":\"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/\",\"name\":\"Come aggiungere un nuovo livello al fattore R (con esempio) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-15T00:45:54+00:00\",\"dateModified\":\"2023-07-15T00:45:54+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come aggiungere un nuovo livello ad un fattore in R, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come aggiungere un nuovo livello al fattore r (con esempio)\"}]},{\"@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 aggiungere un nuovo livello al fattore R (con esempio) - Statorials","description":"Questo tutorial spiega come aggiungere un nuovo livello ad un fattore in R, con un esempio.","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-aggiungi-un-nuovo-livello-di-fattore\/","og_locale":"it_IT","og_type":"article","og_title":"Come aggiungere un nuovo livello al fattore R (con esempio) - Statorials","og_description":"Questo tutorial spiega come aggiungere un nuovo livello ad un fattore in R, con un esempio.","og_url":"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/","og_site_name":"Statorials","article_published_time":"2023-07-15T00:45:54+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-aggiungi-un-nuovo-livello-di-fattore\/","url":"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/","name":"Come aggiungere un nuovo livello al fattore R (con esempio) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-15T00:45:54+00:00","dateModified":"2023-07-15T00:45:54+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come aggiungere un nuovo livello ad un fattore in R, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/r-aggiungi-un-nuovo-livello-di-fattore\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come aggiungere un nuovo livello al fattore r (con esempio)"}]},{"@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\/3878"}],"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=3878"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3878\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}