{"id":2982,"date":"2023-07-19T19:08:39","date_gmt":"2023-07-19T19:08:39","guid":{"rendered":"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/"},"modified":"2023-07-19T19:08:39","modified_gmt":"2023-07-19T19:08:39","slug":"linhas-de-leitura-em-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/","title":{"rendered":"Como usar a fun\u00e7\u00e3o readlines() em r (com exemplos)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">A fun\u00e7\u00e3o <strong>readLines()<\/strong> em R pode ser usada para ler todas ou parte das linhas de texto de um objeto de conex\u00e3o.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Esta fun\u00e7\u00e3o usa a seguinte sintaxe:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong>readLines(con, n=-1L)<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Ouro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>desvantagem:<\/strong> um objeto de conex\u00e3o ou uma sequ\u00eancia de caracteres<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>n:<\/strong> O n\u00famero m\u00e1ximo de linhas a serem lidas. O padr\u00e3o \u00e9 ler todas as linhas.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Os exemplos a seguir mostram como usar esta fun\u00e7\u00e3o na pr\u00e1tica com o seguinte arquivo de texto chamado <strong>some_data.txt<\/strong> :<\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-25801\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/lignes-de-lecture1.jpg\" alt=\"fun\u00e7\u00e3o readLines em R\" width=\"470\" height=\"340\" srcset=\"\" sizes=\"auto, \"><\/p>\n<h3> <strong><span style=\"color: #000000;\">Exemplo 1: Use readLines() para ler todas as linhas de um arquivo de texto<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Digamos que o arquivo de texto esteja salvo na pasta <strong>Documentos<\/strong> do meu computador.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Posso usar a seguinte fun\u00e7\u00e3o <strong>readLines()<\/strong> para ler cada linha deste arquivo de texto:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#read every line from some_data.txt\n<\/span>readLines(\"C:\/Users\/Bob\/Documents\/some_data.txt\")\n\n[1] \u201cThe first line of the file\u201d \u201cThe second line of the file\u201d\n[3] \u201cThe third line of the file\u201d \u201cThe fourth line of the file\u201d\n[5] \"The fifth line of the file\" \"The sixth line of the file\"  \n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">O arquivo de texto cont\u00e9m 6 linhas, ent\u00e3o a fun\u00e7\u00e3o <strong>readLines()<\/strong> produz um vetor de caracteres de comprimento 6.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se eu quiser, posso salvar as linhas do arquivo de texto em um quadro de dados:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#read every line from some_data.txt\n<\/span>my_data &lt;- readLines(\"C:\/Users\/Bob\/Documents\/some_data.txt\")\n\n<span style=\"color: #008080;\">#create data frame\n<\/span>df = data. <span style=\"color: #3366ff;\">frame<\/span> (values=my_data)\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n                       values\n1 The first line of the file\n2 The second line of the file\n3 The third line of the file\n4 The fourth line of the file\n5 The fifth line of the file\n6 The sixth line of the file<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">O resultado \u00e9 um quadro de dados com uma coluna e seis linhas.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Exemplo 2: Use readLines() para ler as primeiras N linhas de um arquivo de texto<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Vamos supor novamente que o arquivo de texto esteja salvo na pasta <strong>Documentos<\/strong> do meu computador.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Posso usar a seguinte fun\u00e7\u00e3o <strong>readLines()<\/strong> com argumento <strong>n<\/strong> para ler apenas as primeiras n linhas deste arquivo de texto:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#read first 4 lines from some_data.txt\n<\/span>readLines(\"C:\/Users\/Bob\/Documents\/some_data.txt\", n= <span style=\"color: #008000;\">4<\/span> )\n\n[1] \u201cThe first line of the file\u201d \u201cThe second line of the file\u201d\n[3] \u201cThe third line of the file\u201d \u201cThe fourth line of the file\u201d\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">A fun\u00e7\u00e3o <strong>readLines()<\/strong> produz um vetor de caracteres de comprimento 4.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tamb\u00e9m posso usar colchetes para navegar at\u00e9 uma linha espec\u00edfica neste arquivo de texto.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Por exemplo, posso usar o seguinte c\u00f3digo para acessar apenas a segunda linha do vetor de caracteres:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#read first 4 lines from some_data.txt\n<\/span>my_data &lt;- readLines(\"C:\/Users\/Bob\/Documents\/some_data.txt\", n= <span style=\"color: #008000;\">4<\/span> )\n\n<span style=\"color: #008080;\">#display second line only\n<\/span>my_data[2]\n\n[1] \"The second line of the file\"\n<\/strong><\/span><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Os tutoriais a seguir explicam como importar outros tipos de arquivo para R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/pt\/mesa-de-leitura\/\" target=\"_blank\" rel=\"noopener\">Como usar read.table em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/importar-csv-para-r\/\" target=\"_blank\" rel=\"noopener\">Como importar arquivos CSV para R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/importar-excel-para-r\/\" target=\"_blank\" rel=\"noopener\">Como importar arquivos Excel para R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A fun\u00e7\u00e3o readLines() em R pode ser usada para ler todas ou parte das linhas de texto de um objeto de conex\u00e3o. Esta fun\u00e7\u00e3o usa a seguinte sintaxe: readLines(con, n=-1L) Ouro: desvantagem: um objeto de conex\u00e3o ou uma sequ\u00eancia de caracteres n: O n\u00famero m\u00e1ximo de linhas a serem lidas. O padr\u00e3o \u00e9 ler todas [&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-2982","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 readLines() em R (com exemplos) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como usar a fun\u00e7\u00e3o readLines() 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\/linhas-de-leitura-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 readLines() em R (com exemplos) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como usar a fun\u00e7\u00e3o readLines() em R, com v\u00e1rios exemplos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-19T19:08:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/lignes-de-lecture1.jpg\" \/>\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=\"3 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/\",\"url\":\"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/\",\"name\":\"Como usar a fun\u00e7\u00e3o readLines() em R (com exemplos) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-19T19:08:39+00:00\",\"dateModified\":\"2023-07-19T19:08:39+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como usar a fun\u00e7\u00e3o readLines() em R, com v\u00e1rios exemplos.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/linhas-de-leitura-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 readlines() 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 a fun\u00e7\u00e3o readLines() em R (com exemplos) \u2013 Statorials","description":"Este tutorial explica como usar a fun\u00e7\u00e3o readLines() 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\/linhas-de-leitura-em-r\/","og_locale":"pt_PT","og_type":"article","og_title":"Como usar a fun\u00e7\u00e3o readLines() em R (com exemplos) \u2013 Statorials","og_description":"Este tutorial explica como usar a fun\u00e7\u00e3o readLines() em R, com v\u00e1rios exemplos.","og_url":"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/","og_site_name":"Statorials","article_published_time":"2023-07-19T19:08:39+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/lignes-de-lecture1.jpg"}],"author":"Dr. benjamim anderson","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"Dr. benjamim anderson","Tempo estimado de leitura":"3 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/","url":"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/","name":"Como usar a fun\u00e7\u00e3o readLines() em R (com exemplos) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-19T19:08:39+00:00","dateModified":"2023-07-19T19:08:39+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como usar a fun\u00e7\u00e3o readLines() em R, com v\u00e1rios exemplos.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/linhas-de-leitura-em-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/linhas-de-leitura-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 readlines() 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\/2982","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=2982"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/2982\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=2982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=2982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=2982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}