{"id":1913,"date":"2023-07-24T11:46:40","date_gmt":"2023-07-24T11:46:40","guid":{"rendered":"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/"},"modified":"2023-07-24T11:46:40","modified_gmt":"2023-07-24T11:46:40","slug":"compare-dois-vetores-em-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/","title":{"rendered":"Como comparar dois vetores em r (com exemplos)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Voc\u00ea pode usar a seguinte sintaxe b\u00e1sica para comparar dois vetores em R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#check if two vectors are identical<\/span>\nidentical(vector_1, vector_2)\n\n<span style=\"color: #008080;\">#display items that are in both vectors\n<\/span>intersect(vector_1, vector_2)\n\n<span style=\"color: #008080;\">#display items that are only in first vector, but not in second vector<\/span>\nsetdiff(vector_1, vector_2)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Os exemplos a seguir mostram como usar essa sintaxe na pr\u00e1tica.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo 1: Verifique se dois vetores s\u00e3o iguais<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como usar a fun\u00e7\u00e3o <strong>id\u00eantica()<\/strong> para verificar se dois vetores s\u00e3o id\u00eanticos:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#definevectors\n<span style=\"color: #000000;\">vector_1 &lt;- c('Andy', 'Bob', 'Carl', 'Doug')\nvector_2 &lt;- c('Bob', 'Carl', 'Doug', 'Ethan', 'Fred')<\/span>\n\n#check if two vectors are identical<\/span>\nidentical(vector_1, vector_2)\n\n[1] FALSE\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Os dois vetores n\u00e3o s\u00e3o id\u00eanticos, portanto um valor <strong>FALSE<\/strong> \u00e9 retornado.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo 2: Encontre elementos que existem em ambos os vetores<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como usar a fun\u00e7\u00e3o <strong>intersect()<\/strong> para exibir elementos que existem em ambos os vetores:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#definevectors\n<span style=\"color: #000000;\">vector_1 &lt;- c('Andy', 'Bob', 'Carl', 'Doug')\nvector_2 &lt;- c('Bob', 'Carl', 'Doug', 'Ethan', 'Fred')<\/span>\n\n#display items that exist in both vectors<\/span>\nintersect(vector_1, vector_2)\n\n[1] \u201cBob\u201d \u201cCarl\u201d \u201cDoug\u201d\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Os tr\u00eas elementos que existem em ambos os vetores s\u00e3o mostrados.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tamb\u00e9m podemos usar a fun\u00e7\u00e3o <strong>length()<\/strong> se quisermos simplesmente saber <em>quantos<\/em> elementos existem nos dois vetores:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#find how many items exist in both vectors\n<span style=\"color: #000000;\">length(intersect(vector_1, vector_2))\n\n[1] 3<\/span>\n<\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Existem tr\u00eas elementos em ambos os vetores.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo 3: Encontre elementos que existem apenas em um \u00fanico vetor<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como usar a fun\u00e7\u00e3o <strong>setdiff()<\/strong> para exibir elementos que existem no primeiro vetor, mas n\u00e3o no segundo:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#definevectors\n<span style=\"color: #000000;\">vector_1 &lt;- c('Andy', 'Bob', 'Carl', 'Doug')\nvector_2 &lt;- c('Bob', 'Carl', 'Doug', 'Ethan', 'Fred')<\/span>\n\n#display items that exist in first vector, but not in second vector<\/span>\nsetdiff(vector_1, vector_2)\n\n[1] \u201cAndy\u201d\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Existe exatamente um elemento no primeiro vetor que n\u00e3o existe no segundo vetor.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Podemos inverter os dois vetores para identificar elementos que existem no segundo vetor, mas n\u00e3o no primeiro:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#definevectors\n<span style=\"color: #000000;\">vector_1 &lt;- c('Andy', 'Bob', 'Carl', 'Doug')\nvector_2 &lt;- c('Bob', 'Carl', 'Doug', 'Ethan', 'Fred')<\/span>\n\n#display items that exist in second vector, but not in first vector<\/span>\nsetdiff(vector_2, vector_1)\n\n[1] \u201cEthan\u201d \u201cFred\u201d\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Existem dois elementos no segundo vetor que n\u00e3o existem no primeiro.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Os tutoriais a seguir explicam como realizar outras tarefas comuns em R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/pt\/comparar-duas-colunas-em-r\/\" target=\"_blank\" rel=\"noopener\">Como comparar duas colunas em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/comparar-strings-em-r\/\">Como comparar strings em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/r-adiciona-ao-vetor-em-loop\/\" target=\"_blank\" rel=\"noopener\">Como adicionar valores a um vetor usando um loop em R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voc\u00ea pode usar a seguinte sintaxe b\u00e1sica para comparar dois vetores em R: #check if two vectors are identical identical(vector_1, vector_2) #display items that are in both vectors intersect(vector_1, vector_2) #display items that are only in first vector, but not in second vector setdiff(vector_1, vector_2) Os exemplos a seguir mostram como usar essa sintaxe na [&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-1913","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 comparar dois vetores em R (com exemplos) \u2013 Estatoriais<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como comparar dois vetores para verificar diferen\u00e7as 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\/compare-dois-vetores-em-r\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como comparar dois vetores em R (com exemplos) \u2013 Estatoriais\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como comparar dois vetores para verificar diferen\u00e7as em R, com v\u00e1rios exemplos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-24T11:46:40+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\/compare-dois-vetores-em-r\/\",\"url\":\"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/\",\"name\":\"Como comparar dois vetores em R (com exemplos) \u2013 Estatoriais\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-24T11:46:40+00:00\",\"dateModified\":\"2023-07-24T11:46:40+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como comparar dois vetores para verificar diferen\u00e7as em R, com v\u00e1rios exemplos.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como comparar dois vetores 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 comparar dois vetores em R (com exemplos) \u2013 Estatoriais","description":"Este tutorial explica como comparar dois vetores para verificar diferen\u00e7as 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\/compare-dois-vetores-em-r\/","og_locale":"pt_PT","og_type":"article","og_title":"Como comparar dois vetores em R (com exemplos) \u2013 Estatoriais","og_description":"Este tutorial explica como comparar dois vetores para verificar diferen\u00e7as em R, com v\u00e1rios exemplos.","og_url":"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/","og_site_name":"Statorials","article_published_time":"2023-07-24T11:46:40+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\/compare-dois-vetores-em-r\/","url":"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/","name":"Como comparar dois vetores em R (com exemplos) \u2013 Estatoriais","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-24T11:46:40+00:00","dateModified":"2023-07-24T11:46:40+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como comparar dois vetores para verificar diferen\u00e7as em R, com v\u00e1rios exemplos.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/compare-dois-vetores-em-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como comparar dois vetores 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\/1913","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=1913"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/1913\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=1913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=1913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=1913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}