{"id":1412,"date":"2023-07-26T11:55:18","date_gmt":"2023-07-26T11:55:18","guid":{"rendered":"https:\/\/statorials.org\/pt\/str_replace-em-r\/"},"modified":"2023-07-26T11:55:18","modified_gmt":"2023-07-26T11:55:18","slug":"str_replace-em-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/str_replace-em-r\/","title":{"rendered":"Como usar str_replace em r (com exemplos)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">A fun\u00e7\u00e3o <strong>str_replace()<\/strong> do pacote <a href=\"https:\/\/stringr.tidyverse.org\/\" target=\"_blank\" rel=\"noopener\">stringr<\/a> em R pode ser usada para substituir padr\u00f5es correspondentes em uma string. Esta fun\u00e7\u00e3o usa a seguinte sintaxe:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>str_replace(string, padr\u00e3o, substitui\u00e7\u00e3o)<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Ouro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>string:<\/strong> vetor de caracteres<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>model:<\/strong> Modelo a ser pesquisado<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>substitui\u00e7\u00e3o:<\/strong> um vetor de caracteres de substitui\u00e7\u00e3o<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Este tutorial fornece v\u00e1rios exemplos de uso pr\u00e1tico desta fun\u00e7\u00e3o no seguinte quadro de dados:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create data frame<\/span>\ndf &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (team=c('team_A', 'team_B', 'team_C', 'team_D'),\n                 conference=c('West', 'West', 'East', 'East'),\n                 dots=c(88, 97, 94, 104))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n    team conference points\n1 team_A West 88\n2 team_B West 97\n3 team_C East 94\n4 team_D East 104<\/strong><\/pre>\n<h2> <strong><span style=\"color: #000000;\">Exemplo 1: Substitua a string por um padr\u00e3o<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como substituir a string \u201cWest\u201d por \u201cWestern\u201d na coluna de confer\u00eancia:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #993300;\">library<\/span> (stringr)<\/span>\n\n#replace \"West\" with \"Western\" in the conference column<\/span>\ndf$conference &lt;- <span style=\"color: #3366ff;\">str_replace<\/span> (df$conference, \" <span style=\"color: #008000;\">West<\/span> \", \" <span style=\"color: #008000;\">Western<\/span> \")\n\n<span style=\"color: #008080;\">#view data frame\n<span style=\"color: #000000;\">df<\/span>\n\n<span style=\"color: #000000;\">team conference points\n1 team_A Western 88\n2 team_B Western 97\n3 team_C East 94\n4 team_D East 104\n<\/span><\/span><\/strong><\/pre>\n<h2> <strong><span style=\"color: #000000;\">Exemplo 2: Substitua string por nada<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como substituir a string \u201cteam_\u201d por nada na coluna team:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#replace \"team_\" with nothing in the team column<\/span>\ndf$team&lt;- <span style=\"color: #3366ff;\">str_replace<\/span> (df$team, \" <span style=\"color: #008000;\">team_<\/span> \", \"\")\n\n<span style=\"color: #008080;\">#view data frame\n<span style=\"color: #000000;\">df\n\n  team conference points\n1 A West 88\n2 B West 97\n3C East 94\n4D East 104\n<\/span><\/span><\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Exemplo 3: Substitua v\u00e1rias strings<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como substituir v\u00e1rias strings em uma \u00fanica coluna. Especificamente:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Mude \u201cOeste\u201d para \u201cW\u201d<\/span><\/li>\n<li> <span style=\"color: #000000;\">Substitua \u201cEst\u201d por \u201cE\u201d<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Como estamos substituindo v\u00e1rias strings, usamos a fun\u00e7\u00e3o <strong>str_replace_all()<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#replace multiple words in the conference column<\/span>\ndf$conference &lt;- <span style=\"color: #3366ff;\">str_replace_all<\/span> (df$conference, c(\" <span style=\"color: #008000;\">West<\/span> \" = \" <span style=\"color: #008000;\">W<\/span> \", \" <span style=\"color: #008000;\">East<\/span> \" = \" <span style=\"color: #008000;\">E<\/span> \"))\n\n<span style=\"color: #008080;\">#view data frame\n<span style=\"color: #000000;\">df\n\n    team conference points\n1 team_A W 88\n2 team_B W 97\n3 team_C E 94\n4 team_D E 104<\/span><\/span><\/strong><\/pre>\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 em R:<\/span><\/p>\n<p> Como realizar correspond\u00eancia parcial de strings em R<br \/><a href=\"https:\/\/statorials.org\/pt\/converter-strings-em-datas-em-r\/\" target=\"_blank\" rel=\"noopener\">Como converter strings em datas em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/caractere-numerico-em-r\/\" target=\"_blank\" rel=\"noopener\">Como converter caractere em num\u00e9rico em R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A fun\u00e7\u00e3o str_replace() do pacote stringr em R pode ser usada para substituir padr\u00f5es correspondentes em uma string. Esta fun\u00e7\u00e3o usa a seguinte sintaxe: str_replace(string, padr\u00e3o, substitui\u00e7\u00e3o) Ouro: string: vetor de caracteres model: Modelo a ser pesquisado substitui\u00e7\u00e3o: um vetor de caracteres de substitui\u00e7\u00e3o Este tutorial fornece v\u00e1rios exemplos de uso pr\u00e1tico desta fun\u00e7\u00e3o no [&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-1412","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 usar str_replace em R (com exemplos) \u2013 Estatoriais<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como usar a fun\u00e7\u00e3o str_replace 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\/str_replace-em-r\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como usar str_replace em R (com exemplos) \u2013 Estatoriais\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como usar a fun\u00e7\u00e3o str_replace em R, com v\u00e1rios exemplos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/str_replace-em-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-26T11:55:18+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\/str_replace-em-r\/\",\"url\":\"https:\/\/statorials.org\/pt\/str_replace-em-r\/\",\"name\":\"Como usar str_replace em R (com exemplos) \u2013 Estatoriais\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-26T11:55:18+00:00\",\"dateModified\":\"2023-07-26T11:55:18+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como usar a fun\u00e7\u00e3o str_replace em R, com v\u00e1rios exemplos.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/str_replace-em-r\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/str_replace-em-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/str_replace-em-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como usar str_replace 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 usar str_replace em R (com exemplos) \u2013 Estatoriais","description":"Este tutorial explica como usar a fun\u00e7\u00e3o str_replace 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\/str_replace-em-r\/","og_locale":"pt_PT","og_type":"article","og_title":"Como usar str_replace em R (com exemplos) \u2013 Estatoriais","og_description":"Este tutorial explica como usar a fun\u00e7\u00e3o str_replace em R, com v\u00e1rios exemplos.","og_url":"https:\/\/statorials.org\/pt\/str_replace-em-r\/","og_site_name":"Statorials","article_published_time":"2023-07-26T11:55:18+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\/str_replace-em-r\/","url":"https:\/\/statorials.org\/pt\/str_replace-em-r\/","name":"Como usar str_replace em R (com exemplos) \u2013 Estatoriais","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-26T11:55:18+00:00","dateModified":"2023-07-26T11:55:18+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como usar a fun\u00e7\u00e3o str_replace em R, com v\u00e1rios exemplos.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/str_replace-em-r\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/str_replace-em-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/str_replace-em-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como usar str_replace 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\/1412","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=1412"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/1412\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=1412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=1412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=1412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}