{"id":3795,"date":"2023-07-15T12:24:30","date_gmt":"2023-07-15T12:24:30","guid":{"rendered":"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/"},"modified":"2023-07-15T12:24:30","modified_gmt":"2023-07-15T12:24:30","slug":"fator-de-mutacao-dplyr","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/","title":{"rendered":"Dplyr: como alterar os n\u00edveis dos fatores usando mutate()"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Voc\u00ea pode usar a seguinte sintaxe b\u00e1sica em <a href=\"https:\/\/dplyr.tidyverse.org\/\" target=\"_blank\" rel=\"noopener\">dplyr<\/a> para alterar os n\u00edveis de uma vari\u00e1vel de fator usando a fun\u00e7\u00e3o <strong>mutate()<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (dplyr)\n\ndf &lt;- df %&gt;% mutate(team=recode(team,\n                                ' <span style=\"color: #ff0000;\">H<\/span> ' = ' <span style=\"color: #ff0000;\">Hawks<\/span> ',\n                                ' <span style=\"color: #ff0000;\">M<\/span> ' = ' <span style=\"color: #ff0000;\">Mavs<\/span> ',\n                                ' <span style=\"color: #ff0000;\">C<\/span> ' = ' <span style=\"color: #ff0000;\">Cavs<\/span> '))\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Esta sintaxe espec\u00edfica faz as seguintes altera\u00e7\u00f5es na vari\u00e1vel de <strong>equipe<\/strong> no quadro de dados:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">&#8216;H&#8217; se torna &#8216;Falc\u00f5es&#8217;<\/span><\/li>\n<li> <span style=\"color: #000000;\">&#8216;M&#8217; se torna &#8216;Mavs&#8217;<\/span><\/li>\n<li> <span style=\"color: #000000;\">&#8216;C&#8217; se torna &#8216;Cavs&#8217;<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">O exemplo a seguir mostra como usar essa sintaxe na pr\u00e1tica.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Exemplo: alterar os n\u00edveis dos fatores usando mutate()<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Suponha que temos o seguinte quadro de dados em R que cont\u00e9m informa\u00e7\u00f5es sobre v\u00e1rios jogadores de basquete:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create data frame\n<\/span>df &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (team=factor(c('H', 'H', 'M', 'M', 'C', 'C')),\n                 dots=c(22, 35, 19, 15, 29, 23))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n  team points\n1:22 a.m.\n2:35 a.m.\n3 M 19\n4 M 15\n5 C 29\n6 C 23\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Podemos usar a seguinte sintaxe com a fun\u00e7\u00e3o <strong>mutate()<\/strong> do pacote <strong>dplyr<\/strong> para alterar os n\u00edveis da vari\u00e1vel <strong>team<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (dplyr)\n\n<span style=\"color: #008080;\">#change factor levels of team variable\n<\/span>df &lt;- df %&gt;% mutate(team=recode(team,\n                                ' <span style=\"color: #ff0000;\">H<\/span> ' = ' <span style=\"color: #ff0000;\">Hawks<\/span> ',\n                                ' <span style=\"color: #ff0000;\">M<\/span> ' = ' <span style=\"color: #ff0000;\">Mavs<\/span> ',\n                                ' <span style=\"color: #ff0000;\">C<\/span> ' = ' <span style=\"color: #ff0000;\">Cavs<\/span> '))\n\n<span style=\"color: #008080;\">#view updated data frame<\/span>\ndf\n\n   team points\n1 Hawks 22\n2 Hawks 35\n3 Mavs 19\n4 Mavs 15\n5 Cavs 29\n6 Cavs 23\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Usando esta sintaxe, conseguimos fazer as<\/span> <span style=\"color: #000000;\">seguintes altera\u00e7\u00f5es na vari\u00e1vel de <strong>equipe<\/strong> no quadro de dados:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">&#8216;H&#8217; se torna &#8216;Falc\u00f5es&#8217;<\/span><\/li>\n<li> <span style=\"color: #000000;\">&#8216;M&#8217; se torna &#8216;Mavs&#8217;<\/span><\/li>\n<li> <span style=\"color: #000000;\">&#8216;C&#8217; se torna &#8216;Cavs&#8217;<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Podemos verificar se os n\u00edveis dos fatores foram alterados usando a fun\u00e7\u00e3o n\u00edveis <strong>()<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#display factor levels of team variable\n<\/span>levels(df$team)\n\n[1] \u201cCavs\u201d \u201cHawks\u201d \u201cMavs\u201d \n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Observe tamb\u00e9m que voc\u00ea pode optar por alterar apenas um n\u00edvel de fator em vez de todos eles.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Por exemplo, podemos usar a seguinte sintaxe para substituir apenas \u201cH\u201d por \u201cHawks\u201d e deixar os outros n\u00edveis de fator inalterados:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (dplyr)\n\n<span style=\"color: #008080;\">#change one factor level of team variable\n<\/span>df &lt;- df %&gt;% mutate(team=recode(team, ' <span style=\"color: #ff0000;\">H<\/span> ' = ' <span style=\"color: #ff0000;\">Hawks<\/span> '))\n\n<span style=\"color: #008080;\">#view updated data frame<\/span>\ndf\n\n   team points\n1 Hawks 22\n2 Hawks 35\n3 M 19\n4 M 15\n5 C 29\n6 C 23\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Observe que \u201cH\u201d foi substitu\u00eddo por \u201cHawks\u201d, mas os outros dois n\u00edveis de fator permaneceram inalterados.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Os tutoriais a seguir explicam como realizar outras tarefas comuns no dplyr:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/pt\/dplyr-excluir-linhas\/\" target=\"_blank\" rel=\"noopener\">Como deletar linhas usando dplyr<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/dplyr-seleciona-colunas-por-indice\/\" target=\"_blank\" rel=\"noopener\">Como selecionar colunas por \u00edndice usando dplyr<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/filtrar-linhas-que-contem-string-dplyr\/\" target=\"_blank\" rel=\"noopener\">Como filtrar linhas contendo uma determinada string usando dplyr<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voc\u00ea pode usar a seguinte sintaxe b\u00e1sica em dplyr para alterar os n\u00edveis de uma vari\u00e1vel de fator usando a fun\u00e7\u00e3o mutate() : library (dplyr) df &lt;- df %&gt;% mutate(team=recode(team, &#8216; H &#8216; = &#8216; Hawks &#8216;, &#8216; M &#8216; = &#8216; Mavs &#8216;, &#8216; C &#8216; = &#8216; Cavs &#8216;)) Esta sintaxe espec\u00edfica faz [&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":[],"class_list":["post-3795","post","type-post","status-publish","format-standard","hentry","category-guia"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>dplyr: Como alterar os n\u00edveis dos fatores usando mutate() - Estatologia<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como usar a fun\u00e7\u00e3o mutate() em dplyr com fatores, incluindo um exemplo.\" \/>\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\/pt\/fator-de-mutacao-dplyr\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"dplyr: Como alterar os n\u00edveis dos fatores usando mutate() - Estatologia\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como usar a fun\u00e7\u00e3o mutate() em dplyr com fatores, incluindo um exemplo.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-15T12:24:30+00:00\" \/>\n<meta name=\"author\" content=\"Dr. benjamim anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr. benjamim anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tempo estimado de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/\",\"url\":\"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/\",\"name\":\"dplyr: Como alterar os n\u00edveis dos fatores usando mutate() - Estatologia\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-15T12:24:30+00:00\",\"dateModified\":\"2023-07-15T12:24:30+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como usar a fun\u00e7\u00e3o mutate() em dplyr com fatores, incluindo um exemplo.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dplyr: como alterar os n\u00edveis dos fatores usando mutate()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/pt\/#website\",\"url\":\"https:\/\/statorials.org\/pt\/\",\"name\":\"Statorials\",\"description\":\"O seu guia para a literacia estat\u00edstica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/pt\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pt-PT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\",\"name\":\"Dr. benjamim anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-PT\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. benjamim anderson\"},\"description\":\"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais\",\"sameAs\":[\"https:\/\/statorials.org\/pt\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"dplyr: Como alterar os n\u00edveis dos fatores usando mutate() - Estatologia","description":"Este tutorial explica como usar a fun\u00e7\u00e3o mutate() em dplyr com fatores, incluindo um exemplo.","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\/pt\/fator-de-mutacao-dplyr\/","og_locale":"pt_PT","og_type":"article","og_title":"dplyr: Como alterar os n\u00edveis dos fatores usando mutate() - Estatologia","og_description":"Este tutorial explica como usar a fun\u00e7\u00e3o mutate() em dplyr com fatores, incluindo um exemplo.","og_url":"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/","og_site_name":"Statorials","article_published_time":"2023-07-15T12:24:30+00:00","author":"Dr. benjamim anderson","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"Dr. benjamim anderson","Tempo estimado de leitura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/","url":"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/","name":"dplyr: Como alterar os n\u00edveis dos fatores usando mutate() - Estatologia","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-15T12:24:30+00:00","dateModified":"2023-07-15T12:24:30+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como usar a fun\u00e7\u00e3o mutate() em dplyr com fatores, incluindo um exemplo.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/fator-de-mutacao-dplyr\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Dplyr: como alterar os n\u00edveis dos fatores usando mutate()"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/pt\/#website","url":"https:\/\/statorials.org\/pt\/","name":"Statorials","description":"O seu guia para a literacia estat\u00edstica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/pt\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pt-PT"},{"@type":"Person","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666","name":"Dr. benjamim anderson","image":{"@type":"ImageObject","inLanguage":"pt-PT","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr. benjamim anderson"},"description":"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais","sameAs":["https:\/\/statorials.org\/pt"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/comments?post=3795"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3795\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=3795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=3795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=3795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}