{"id":1788,"date":"2023-07-24T23:44:29","date_gmt":"2023-07-24T23:44:29","guid":{"rendered":"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/"},"modified":"2023-07-24T23:44:29","modified_gmt":"2023-07-24T23:44:29","slug":"substitua-na-por-string-em-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/","title":{"rendered":"Como substituir nas por strings em r (com exemplos)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Voc\u00ea pode usar a fun\u00e7\u00e3o <strong>replace_na()<\/strong> do pacote <strong>Tidyr<\/strong> para substituir NAs por strings espec\u00edficas em uma coluna de um quadro de dados em R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#replace NA values in column x with \"missing\"\n<span style=\"color: #000000;\">df$x %&gt;% <span style=\"color: #3366ff;\">replace_na<\/span> (' <span style=\"color: #ff0000;\">none<\/span> ')<\/span><\/span>\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Voc\u00ea tamb\u00e9m pode usar esta fun\u00e7\u00e3o para substituir NAs por strings espec\u00edficas em m\u00faltiplas colunas de um quadro de dados:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#replace NA values in column x with \"missing\" and NA values in column y with \"none\"<\/span>\ndf %&gt;% <span style=\"color: #3366ff;\">replace_na<\/span> (list(x = ' <span style=\"color: #ff0000;\">missing<\/span> ', y = ' <span style=\"color: #ff0000;\">none<\/span> '))<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Os exemplos a seguir mostram como usar esta fun\u00e7\u00e3o na pr\u00e1tica.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo 1: Substitua NAs por strings em uma coluna<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como substituir NAs por uma string espec\u00edfica em uma coluna de um quadro de dados:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #993300;\">library<\/span> (tidyr)\n\ndf &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (status=c('single', 'married', 'married', NA),\n                 education=c('Assoc', 'Bach', NA, 'Master'),\n                 income=c(34, 88, 92, 90))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n   status education income\n1 single Assoc 34\n2 married Bach 88\n3 married &lt;NA&gt; 92\n4 &lt;NA&gt; Master 90\n\n<span style=\"color: #008080;\">#replace missing values with 'single' in status column\n<\/span>df$status &lt;- df$status %&gt;% <span style=\"color: #3366ff;\">replace_na<\/span> (' <span style=\"color: #ff0000;\">single<\/span> ')\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df \n\n   status education income\n1 single Assoc 34\n2 married Bach 88\n3 married &lt;NA&gt; 92\n4 single Master 90\n<\/strong><\/span><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo 2: Substitua NAs por strings em m\u00faltiplas colunas<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como substituir NAs por uma string espec\u00edfica em m\u00faltiplas colunas de um quadro de dados:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #993300;\">library<\/span> (tidyr)\n\ndf &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (status=c('single', 'married', 'married', NA),\n                 education=c('Assoc', 'Bach', NA, 'Master'),\n                 income=c(34, 88, 92, 90))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n   status education income\n1 single Assoc 34\n2 married Bach 88\n3 married &lt;NA&gt; 92\n4 &lt;NA&gt; Master 90\n\n<span style=\"color: #008080;\">#replace missing values with 'single' in status column\n<\/span>df &lt;- df %&gt;% <span style=\"color: #3366ff;\">replace_na<\/span> (list(status = ' <span style=\"color: #ff0000;\">single<\/span> ', education = ' <span style=\"color: #ff0000;\">none<\/span> '))\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df \n\n   status education income\n1 single Assoc 34\n2 married Bach 88\n3 married none 92\n4 single Master 90<\/strong><\/span><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h3>\n<p> <a href=\"https:\/\/statorials.org\/pt\/excluir-na-em-r\/\" target=\"_blank\" rel=\"noopener\">Como deletar linhas com alguns ou todos os NAs em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/dplyr-substitua-na-por-zero\/\" target=\"_blank\" rel=\"noopener\">Como substituir NA por Zero no dplyr<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voc\u00ea pode usar a fun\u00e7\u00e3o replace_na() do pacote Tidyr para substituir NAs por strings espec\u00edficas em uma coluna de um quadro de dados em R: #replace NA values in column x with &#8220;missing&#8221; df$x %&gt;% replace_na (&#8216; none &#8216;) Voc\u00ea tamb\u00e9m pode usar esta fun\u00e7\u00e3o para substituir NAs por strings espec\u00edficas em m\u00faltiplas colunas de [&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-1788","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 substituir NAs por strings em R (com exemplos)<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como substituir NAs por strings em R, com v\u00e1rios exemplos.\" \/>\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\/substitua-na-por-string-em-r\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como substituir NAs por strings em R (com exemplos)\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como substituir NAs por strings em R, com v\u00e1rios exemplos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-24T23:44:29+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\/substitua-na-por-string-em-r\/\",\"url\":\"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/\",\"name\":\"Como substituir NAs por strings em R (com exemplos)\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-24T23:44:29+00:00\",\"dateModified\":\"2023-07-24T23:44:29+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como substituir NAs por strings em R, com v\u00e1rios exemplos.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como substituir nas por strings em r (com exemplos)\"}]},{\"@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 substituir NAs por strings em R (com exemplos)","description":"Este tutorial explica como substituir NAs por strings em R, com v\u00e1rios exemplos.","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\/substitua-na-por-string-em-r\/","og_locale":"pt_PT","og_type":"article","og_title":"Como substituir NAs por strings em R (com exemplos)","og_description":"Este tutorial explica como substituir NAs por strings em R, com v\u00e1rios exemplos.","og_url":"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/","og_site_name":"Statorials","article_published_time":"2023-07-24T23:44:29+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\/substitua-na-por-string-em-r\/","url":"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/","name":"Como substituir NAs por strings em R (com exemplos)","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-24T23:44:29+00:00","dateModified":"2023-07-24T23:44:29+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como substituir NAs por strings em R, com v\u00e1rios exemplos.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/substitua-na-por-string-em-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como substituir nas por strings em r (com exemplos)"}]},{"@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\/1788","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=1788"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/1788\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=1788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=1788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=1788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}