{"id":2905,"date":"2023-07-20T04:06:18","date_gmt":"2023-07-20T04:06:18","guid":{"rendered":"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/"},"modified":"2023-07-20T04:06:18","modified_gmt":"2023-07-20T04:06:18","slug":"funcao-de-origem-em-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/","title":{"rendered":"Como usar a fun\u00e7\u00e3o de origem em r (com exemplo)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\"><span style=\"color: #000000;\">Voc\u00ea pode usar a fun\u00e7\u00e3o <strong>de origem<\/strong> em R para reutilizar fun\u00e7\u00f5es criadas em outro script R.<\/span><\/span><\/p>\n<p> <span style=\"color: #000000;\">Esta fun\u00e7\u00e3o usa a seguinte sintaxe b\u00e1sica:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #3366ff;\">source<\/span> (\"path\/to\/some\/file.R\")<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Basta adicionar esta linha ao topo do seu script R e voc\u00ea poder\u00e1 usar todas as fun\u00e7\u00f5es definidas em <strong>file.R.<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">O exemplo a seguir mostra como usar a fun\u00e7\u00e3o <strong>source<\/strong> na pr\u00e1tica.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo: usando a fun\u00e7\u00e3o de origem em R<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Suponha que temos o seguinte script R chamado <strong>some_functions.R<\/strong> que cont\u00e9m duas fun\u00e7\u00f5es simples definidas pelo usu\u00e1rio:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #339966;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#define function that divides values by 2\n<span style=\"color: #000000;\">divide_by_two &lt;- <span style=\"color: #008000;\">function<\/span> (x) {\n  <span style=\"color: #008000;\">return<\/span> (x\/2)\n}<\/span>\n\n#define function that multiplies values by 3\n<span style=\"color: #000000;\">multiply_by_three &lt;- <span style=\"color: #008000;\">function<\/span> (x) {\n  <span style=\"color: #008000;\">return<\/span> (x*3)\n}\n<\/span><\/span><\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Agora digamos que estamos trabalhando atualmente com um script R chamado <strong>main_script.R<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Supondo que <strong>some_functions.R<\/strong> e <strong>main_script.R<\/strong> estejam na mesma pasta, podemos usar source no topo de nosso <strong>main_script.R<\/strong> para nos permitir usar as fun\u00e7\u00f5es que definimos no script <strong>some_functions.R<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #3366ff;\">source<\/span> (\"some_functions.R\")\n\n<span style=\"color: #008080;\">#create data frame\n<\/span>df &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (team=c('A', 'B', 'C', 'D', 'E', 'F'),\n                 points=c(14, 19, 22, 15, 30, 40))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n  team points\n1 to 14\n2 B 19\n3 C 22\n4 D 15\n5 E 30\n6 F 40\n\n<span style=\"color: #008080;\">#create new columns using functions from some_functions.R\n<\/span>df$half_points &lt;- divide_by_two(df$points)\n\ndf$triple_points &lt;- multiply_by_three(df$points)\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n  team points half_points triple_points\n1 A 14 7.0 42\n2 B 19 9.5 57\n3 C 22 11.0 66\n4 D 15 7.5 45\n5 E 30 15.0 90\n6 F 40 20.0 120\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Observe que podemos criar duas novas colunas em nosso quadro de dados usando as fun\u00e7\u00f5es que definimos no script <strong>some_functions.R<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">A fun\u00e7\u00e3o <strong>de origem<\/strong> nos permitiu usar as fun\u00e7\u00f5es <strong>Divide_by_Two<\/strong> e <strong>Multiplier_by_Three<\/strong> em nosso script atual, mesmo que essas fun\u00e7\u00f5es n\u00e3o tenham sido criadas no script atual.<\/span><\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : Neste exemplo, usamos apenas uma fun\u00e7\u00e3o <strong>de origem<\/strong> na parte superior do arquivo. No entanto, podemos usar quantas fun\u00e7\u00f5es <strong>de origem<\/strong> quisermos se quisermos reutilizar fun\u00e7\u00f5es definidas em v\u00e1rios scripts diferentes.<\/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 usar outras fun\u00e7\u00f5es comuns em R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/pt\/veja-funcao-em-r\/\" target=\"_blank\" rel=\"noopener\">Como usar a fun\u00e7\u00e3o View() em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/agregado-r\/\" target=\"_blank\" rel=\"noopener\">Como usar a fun\u00e7\u00e3o Aggregate() em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/substituir-funcao-em-r\/\" target=\"_blank\" rel=\"noopener\">Como usar a fun\u00e7\u00e3o replace() em R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voc\u00ea pode usar a fun\u00e7\u00e3o de origem em R para reutilizar fun\u00e7\u00f5es criadas em outro script R. Esta fun\u00e7\u00e3o usa a seguinte sintaxe b\u00e1sica: source (&#8220;path\/to\/some\/file.R&#8221;) Basta adicionar esta linha ao topo do seu script R e voc\u00ea poder\u00e1 usar todas as fun\u00e7\u00f5es definidas em file.R. O exemplo a seguir mostra como usar a fun\u00e7\u00e3o [&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-2905","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 a fun\u00e7\u00e3o de origem em R (com exemplo) - Estatoriais<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como usar a fun\u00e7\u00e3o source em R, incluindo um exemplo completo.\" \/>\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\/funcao-de-origem-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 a fun\u00e7\u00e3o de origem em R (com exemplo) - Estatoriais\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como usar a fun\u00e7\u00e3o source em R, incluindo um exemplo completo.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-20T04:06: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\/funcao-de-origem-em-r\/\",\"url\":\"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/\",\"name\":\"Como usar a fun\u00e7\u00e3o de origem em R (com exemplo) - Estatoriais\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-20T04:06:18+00:00\",\"dateModified\":\"2023-07-20T04:06:18+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como usar a fun\u00e7\u00e3o source em R, incluindo um exemplo completo.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como usar a fun\u00e7\u00e3o de origem em r (com exemplo)\"}]},{\"@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 a fun\u00e7\u00e3o de origem em R (com exemplo) - Estatoriais","description":"Este tutorial explica como usar a fun\u00e7\u00e3o source em R, incluindo um exemplo completo.","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\/funcao-de-origem-em-r\/","og_locale":"pt_PT","og_type":"article","og_title":"Como usar a fun\u00e7\u00e3o de origem em R (com exemplo) - Estatoriais","og_description":"Este tutorial explica como usar a fun\u00e7\u00e3o source em R, incluindo um exemplo completo.","og_url":"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/","og_site_name":"Statorials","article_published_time":"2023-07-20T04:06: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\/funcao-de-origem-em-r\/","url":"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/","name":"Como usar a fun\u00e7\u00e3o de origem em R (com exemplo) - Estatoriais","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-20T04:06:18+00:00","dateModified":"2023-07-20T04:06:18+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como usar a fun\u00e7\u00e3o source em R, incluindo um exemplo completo.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/funcao-de-origem-em-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como usar a fun\u00e7\u00e3o de origem em r (com exemplo)"}]},{"@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\/2905","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=2905"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/2905\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=2905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=2905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=2905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}