{"id":903,"date":"2023-07-28T08:57:54","date_gmt":"2023-07-28T08:57:54","guid":{"rendered":"https:\/\/statorials.org\/pt\/recodificar-dplyr\/"},"modified":"2023-07-28T08:57:54","modified_gmt":"2023-07-28T08:57:54","slug":"recodificar-dplyr","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/recodificar-dplyr\/","title":{"rendered":"Como recodificar valores usando dplyr"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c0s vezes voc\u00ea pode estar interessado em recodificar certos valores em um quadro de dados em R. Felizmente, isso pode ser feito facilmente usando a fun\u00e7\u00e3o <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/recode.html\" target=\"_blank\" rel=\"noopener\">recode()<\/a> do pacote dplyr.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Este tutorial mostra v\u00e1rios exemplos de uso pr\u00e1tico desta fun\u00e7\u00e3o.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo 1: Recodificar uma \u00fanica coluna em um dataframe<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como recodificar uma \u00fanica coluna em um dataframe:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #000000;\"><span style=\"color: #000000;\">library(dplyr)<\/span>\n<\/span>\n<span style=\"color: #008080;\">#create dataframe<\/span> \ndf &lt;- data.frame(player = c('A', 'B', 'C', 'D'),\n                 points = c(24, 29, 13, 15),\n                 result = c('Win', 'Loss', 'Win', 'Loss'))\n\n<span style=\"color: #008080;\">#view dataframe<\/span> \ndf\n\n<span style=\"color: #008080;\">#change 'Win' and 'Loss' to '1' and '0'<\/span>\ndf %&gt;% <span style=\"color: #3366ff;\">mutate<\/span> (result=recode(result, ' <span style=\"color: #008000;\">Win<\/span> '='1', ' <span style=\"color: #008000;\">Loss<\/span> '='0'))\n\n       player points result\n1 to 24 1\n2 B 29 0\n3 C 13 1\n4 D 15 0\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo 2: Recodificar uma \u00fanica coluna em um quadro de dados e fornecer valores NA<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como recodificar uma \u00fanica coluna em um quadro de dados e atribuir o valor <strong>NA<\/strong> a qualquer valor que n\u00e3o tenha sido atribu\u00eddo explicitamente a um novo valor:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #000000;\">library(dplyr)\n<\/span>\n<span style=\"color: #008080;\">#create dataframe<\/span> \ndf &lt;- data.frame(player = c('A', 'B', 'C', 'D'),\n                 points = c(24, 29, 13, 15),\n                 result = c('Win', 'Loss', 'Win', 'Loss'))\n\n<span style=\"color: #008080;\">#view dataframe<\/span> \ndf\n\n<span style=\"color: #008080;\">#change 'Win' to '1' and give all other values a value of NA<\/span>\ndf %&gt;% <span style=\"color: #3366ff;\">mutate<\/span> (result=recode(result, ' <span style=\"color: #008000;\">Win<\/span> '='1', <span style=\"color: #3366ff;\">.default<\/span> =NA_character_))\n\n       player points result\n1 to 24 1\n2 B 29 &lt;NA&gt;\n3 C 13 1\n4 D 15 &lt;NA&gt;<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo 3: recodificar v\u00e1rias colunas em um dataframe<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como recodificar v\u00e1rias colunas de uma vez em um dataframe:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #000000;\">library(dplyr)\n<\/span>\n<span style=\"color: #008080;\">#create dataframe<\/span> \ndf &lt;- data.frame(player = c('A', 'B', 'C', 'D'),\n                 points = c(24, 29, 13, 15),\n                 result = c('Win', 'Loss', 'Win', 'Loss'))\n\n<span style=\"color: #008080;\">#recode 'player' and 'result' columns<\/span>\ndf %&gt;% <span style=\"color: #3366ff;\">mutate<\/span> (player=recode(player, ' <span style=\"color: #008000;\">A<\/span> '='Z'),\n              result=recode(result, ' <span style=\"color: #008000;\">Win<\/span> '='1', ' <span style=\"color: #008000;\">Loss<\/span> '='0'))\n\n       player points result\n1 Z 24 1\n2 B 29 0\n3 C 13 1\n4 D 15 0<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><em>Voc\u00ea pode encontrar a documenta\u00e7\u00e3o completa da fun\u00e7\u00e3o recode() <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/recode.html\" target=\"_blank\" rel=\"noopener\">aqui<\/a> .<\/em><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c0s vezes voc\u00ea pode estar interessado em recodificar certos valores em um quadro de dados em R. Felizmente, isso pode ser feito facilmente usando a fun\u00e7\u00e3o recode() do pacote dplyr. Este tutorial mostra v\u00e1rios exemplos de uso pr\u00e1tico desta fun\u00e7\u00e3o. Exemplo 1: Recodificar uma \u00fanica coluna em um dataframe O c\u00f3digo a seguir mostra como [&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-903","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>Como recodificar valores usando dplyr \u2013 Estatologia<\/title>\n<meta name=\"description\" content=\"Uma explica\u00e7\u00e3o simples de como recodificar valores em R usando a fun\u00e7\u00e3o recode() do dplyr.\" \/>\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\/recodificar-dplyr\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como recodificar valores usando dplyr \u2013 Estatologia\" \/>\n<meta property=\"og:description\" content=\"Uma explica\u00e7\u00e3o simples de como recodificar valores em R usando a fun\u00e7\u00e3o recode() do dplyr.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/recodificar-dplyr\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T08:57:54+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\/recodificar-dplyr\/\",\"url\":\"https:\/\/statorials.org\/pt\/recodificar-dplyr\/\",\"name\":\"Como recodificar valores usando dplyr \u2013 Estatologia\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-28T08:57:54+00:00\",\"dateModified\":\"2023-07-28T08:57:54+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Uma explica\u00e7\u00e3o simples de como recodificar valores em R usando a fun\u00e7\u00e3o recode() do dplyr.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/recodificar-dplyr\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/recodificar-dplyr\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/recodificar-dplyr\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como recodificar valores usando dplyr\"}]},{\"@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":"Como recodificar valores usando dplyr \u2013 Estatologia","description":"Uma explica\u00e7\u00e3o simples de como recodificar valores em R usando a fun\u00e7\u00e3o recode() do dplyr.","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\/recodificar-dplyr\/","og_locale":"pt_PT","og_type":"article","og_title":"Como recodificar valores usando dplyr \u2013 Estatologia","og_description":"Uma explica\u00e7\u00e3o simples de como recodificar valores em R usando a fun\u00e7\u00e3o recode() do dplyr.","og_url":"https:\/\/statorials.org\/pt\/recodificar-dplyr\/","og_site_name":"Statorials","article_published_time":"2023-07-28T08:57:54+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\/recodificar-dplyr\/","url":"https:\/\/statorials.org\/pt\/recodificar-dplyr\/","name":"Como recodificar valores usando dplyr \u2013 Estatologia","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-28T08:57:54+00:00","dateModified":"2023-07-28T08:57:54+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Uma explica\u00e7\u00e3o simples de como recodificar valores em R usando a fun\u00e7\u00e3o recode() do dplyr.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/recodificar-dplyr\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/recodificar-dplyr\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/recodificar-dplyr\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como recodificar valores usando dplyr"}]},{"@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\/903","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=903"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/903\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}