{"id":2982,"date":"2023-07-19T19:08:39","date_gmt":"2023-07-19T19:08:39","guid":{"rendered":"https:\/\/statorials.org\/id\/garis-baca-di-r\/"},"modified":"2023-07-19T19:08:39","modified_gmt":"2023-07-19T19:08:39","slug":"garis-baca-di-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/id\/garis-baca-di-r\/","title":{"rendered":"Cara menggunakan fungsi readlines() di r (dengan contoh)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Fungsi <strong>readLines()<\/strong> di R dapat digunakan untuk membaca seluruh atau sebagian baris teks dari objek koneksi.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Fungsi ini menggunakan sintaks berikut:<\/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;\">Emas:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>kerugian:<\/strong> objek koneksi atau string karakter<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>n:<\/strong> Jumlah baris maksimum yang harus dibaca. Standarnya adalah membaca semua baris.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Contoh berikut menunjukkan cara menggunakan fungsi ini dalam praktiknya dengan file teks berikut bernama <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=\"fungsi readLines di R\" width=\"470\" height=\"340\" srcset=\"\" sizes=\"\"><\/p>\n<h3> <strong><span style=\"color: #000000;\">Contoh 1: Gunakan readLines() untuk membaca semua baris dari file teks<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Katakanlah file teks disimpan di folder <strong>Dokumen<\/strong> di komputer saya.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Saya dapat menggunakan fungsi <strong>readLines()<\/strong> berikut untuk membaca setiap baris dari file teks ini:<\/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;\">File teks berisi 6 baris, sehingga fungsi <strong>readLines()<\/strong> menghasilkan vektor karakter dengan panjang 6.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Jika mau, saya bisa menyimpan baris file teks dalam bingkai data:<\/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;\">Hasilnya adalah bingkai data dengan satu kolom dan enam baris.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Contoh 2: Gunakan readLines() untuk membaca N baris pertama file teks<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Mari kita asumsikan lagi bahwa file teks disimpan di folder <strong>Dokumen<\/strong> di komputer saya.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Saya dapat menggunakan fungsi <strong>readLines()<\/strong> berikut dengan argumen <strong>n<\/strong> untuk hanya membaca n baris pertama dari file teks ini:<\/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;\">Fungsi <strong>readLines()<\/strong> menghasilkan vektor karakter dengan panjang 4.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Saya juga dapat menggunakan tanda kurung siku untuk menavigasi ke baris tertentu dalam file teks ini.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Misalnya, saya dapat menggunakan kode berikut untuk mengakses hanya baris kedua dari vektor karakter:<\/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>Sumber daya tambahan<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Tutorial berikut menjelaskan cara mengimpor jenis file lain ke R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/id\/r-meja-baca\/\" target=\"_blank\" rel=\"noopener\">Cara menggunakan read.table di R<\/a><br \/> <a href=\"https:\/\/statorials.org\/id\/impor-csv-ke-r\/\" target=\"_blank\" rel=\"noopener\">Cara mengimpor file CSV ke R<\/a><br \/> <a href=\"https:\/\/statorials.org\/id\/impor-excel-ke-r\/\" target=\"_blank\" rel=\"noopener\">Cara mengimpor file Excel ke R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fungsi readLines() di R dapat digunakan untuk membaca seluruh atau sebagian baris teks dari objek koneksi. Fungsi ini menggunakan sintaks berikut: readLines(con, n=-1L) Emas: kerugian: objek koneksi atau string karakter n: Jumlah baris maksimum yang harus dibaca. Standarnya adalah membaca semua baris. Contoh berikut menunjukkan cara menggunakan fungsi ini dalam praktiknya dengan file teks berikut [&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":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Cara Menggunakan Fungsi readLines() di R (Dengan Contoh) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Tutorial ini menjelaskan cara menggunakan fungsi readLines() di R, dengan beberapa contoh.\" \/>\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\/id\/garis-baca-di-r\/\" \/>\n<meta property=\"og:locale\" content=\"id_ID\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cara Menggunakan Fungsi readLines() di R (Dengan Contoh) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Tutorial ini menjelaskan cara menggunakan fungsi readLines() di R, dengan beberapa contoh.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/id\/garis-baca-di-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=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Ditulis oleh\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimasi waktu membaca\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 menit\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/id\/garis-baca-di-r\/\",\"url\":\"https:\/\/statorials.org\/id\/garis-baca-di-r\/\",\"name\":\"Cara Menggunakan Fungsi readLines() di R (Dengan Contoh) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/id\/#website\"},\"datePublished\":\"2023-07-19T19:08:39+00:00\",\"dateModified\":\"2023-07-19T19:08:39+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/id\/#\/schema\/person\/3d17a1160dd2d052b7c78e502cb9ec81\"},\"description\":\"Tutorial ini menjelaskan cara menggunakan fungsi readLines() di R, dengan beberapa contoh.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/id\/garis-baca-di-r\/#breadcrumb\"},\"inLanguage\":\"id\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/id\/garis-baca-di-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/id\/garis-baca-di-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/statorials.org\/id\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cara menggunakan fungsi readlines() di r (dengan contoh)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/id\/#website\",\"url\":\"https:\/\/statorials.org\/id\/\",\"name\":\"Statorials\",\"description\":\"Panduan anda untuk kompetensi statistik!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/id\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"id\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/id\/#\/schema\/person\/3d17a1160dd2d052b7c78e502cb9ec81\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"id\",\"@id\":\"https:\/\/statorials.org\/id\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/statorials.org\/id\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"http:\/\/statorials.org\/id\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Halo, saya Benjamin, pensiunan profesor statistika yang menjadi guru Statorial yang berdedikasi. Dengan pengalaman dan keahlian yang luas di bidang statistika, saya ingin berbagi ilmu untuk memberdayakan mahasiswa melalui Statorials. Baca selengkapnya\",\"sameAs\":[\"http:\/\/statorials.org\/id\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cara Menggunakan Fungsi readLines() di R (Dengan Contoh) \u2013 Statorials","description":"Tutorial ini menjelaskan cara menggunakan fungsi readLines() di R, dengan beberapa contoh.","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\/id\/garis-baca-di-r\/","og_locale":"id_ID","og_type":"article","og_title":"Cara Menggunakan Fungsi readLines() di R (Dengan Contoh) \u2013 Statorials","og_description":"Tutorial ini menjelaskan cara menggunakan fungsi readLines() di R, dengan beberapa contoh.","og_url":"https:\/\/statorials.org\/id\/garis-baca-di-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":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Ditulis oleh":"Benjamin anderson","Estimasi waktu membaca":"2 menit"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/id\/garis-baca-di-r\/","url":"https:\/\/statorials.org\/id\/garis-baca-di-r\/","name":"Cara Menggunakan Fungsi readLines() di R (Dengan Contoh) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/id\/#website"},"datePublished":"2023-07-19T19:08:39+00:00","dateModified":"2023-07-19T19:08:39+00:00","author":{"@id":"https:\/\/statorials.org\/id\/#\/schema\/person\/3d17a1160dd2d052b7c78e502cb9ec81"},"description":"Tutorial ini menjelaskan cara menggunakan fungsi readLines() di R, dengan beberapa contoh.","breadcrumb":{"@id":"https:\/\/statorials.org\/id\/garis-baca-di-r\/#breadcrumb"},"inLanguage":"id","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/id\/garis-baca-di-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/id\/garis-baca-di-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/statorials.org\/id\/"},{"@type":"ListItem","position":2,"name":"Cara menggunakan fungsi readlines() di r (dengan contoh)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/id\/#website","url":"https:\/\/statorials.org\/id\/","name":"Statorials","description":"Panduan anda untuk kompetensi statistik!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/id\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"id"},{"@type":"Person","@id":"https:\/\/statorials.org\/id\/#\/schema\/person\/3d17a1160dd2d052b7c78e502cb9ec81","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"id","@id":"https:\/\/statorials.org\/id\/#\/schema\/person\/image\/","url":"http:\/\/statorials.org\/id\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"http:\/\/statorials.org\/id\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Halo, saya Benjamin, pensiunan profesor statistika yang menjadi guru Statorial yang berdedikasi. Dengan pengalaman dan keahlian yang luas di bidang statistika, saya ingin berbagi ilmu untuk memberdayakan mahasiswa melalui Statorials. Baca selengkapnya","sameAs":["http:\/\/statorials.org\/id"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/posts\/2982"}],"collection":[{"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/comments?post=2982"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/posts\/2982\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/media?parent=2982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/categories?post=2982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/id\/wp-json\/wp\/v2\/tags?post=2982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}