{"id":2983,"date":"2023-07-19T19:08:39","date_gmt":"2023-07-19T19:08:39","guid":{"rendered":"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/"},"modified":"2023-07-19T19:08:39","modified_gmt":"2023-07-19T19:08:39","slug":"leggi-le-righe-in-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/","title":{"rendered":"Come utilizzare la funzione readlines() in r (con esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">La funzione <strong>readLines()<\/strong> in R pu\u00f2 essere utilizzata per leggere tutte o parte delle righe di testo da un oggetto connessione.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questa funzione utilizza la seguente sintassi:<\/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;\">Oro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>svantaggio:<\/strong> un oggetto di connessione o una stringa di caratteri<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>n:<\/strong> il numero massimo di righe da leggere. L&#8217;impostazione predefinita prevede la lettura di tutte le righe.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">I seguenti esempi mostrano come utilizzare in pratica questa funzione con il seguente file di testo chiamato <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=\"Funzione readLines in R\" width=\"470\" height=\"340\" srcset=\"\" sizes=\"\"><\/p>\n<h3> <strong><span style=\"color: #000000;\">Esempio 1: utilizzare readLines() per leggere tutte le righe da un file di testo<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Supponiamo che il file di testo sia salvato nella cartella <strong>Documenti<\/strong> sul mio computer.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Posso usare la seguente funzione <strong>readLines()<\/strong> per leggere ogni riga da questo file di testo:<\/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;\">Il file di testo contiene 6 righe, quindi la funzione <strong>readLines()<\/strong> produce un vettore di caratteri di lunghezza 6.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se voglio posso invece salvare le righe del file di testo in un data frame:<\/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;\">Il risultato \u00e8 un frame di dati con una colonna e sei righe.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Esempio 2: utilizzare readLines() per leggere le prime N righe di un file di testo<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Supponiamo ancora una volta che il file di testo sia salvato nella cartella <strong>Documenti<\/strong> del mio computer.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Posso usare la seguente funzione <strong>readLines()<\/strong> con argomento <strong>n<\/strong> per leggere solo le prime n righe di questo file di testo:<\/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;\">La funzione <strong>readLines()<\/strong> produce un vettore di caratteri di lunghezza 4.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Posso anche utilizzare le parentesi quadre per passare a una riga specifica in questo file di testo.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ad esempio, posso utilizzare il seguente codice per accedere solo alla seconda riga del vettore di caratteri:<\/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>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come importare altri tipi di file in R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/r-tavolo-da-lettura\/\" target=\"_blank\" rel=\"noopener\">Come utilizzare read.table in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/importare-csv-in-r\/\" target=\"_blank\" rel=\"noopener\">Come importare file CSV in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/importare-excel-in-r\/\" target=\"_blank\" rel=\"noopener\">Come importare file Excel in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>La funzione readLines() in R pu\u00f2 essere utilizzata per leggere tutte o parte delle righe di testo da un oggetto connessione. Questa funzione utilizza la seguente sintassi: readLines(con, n=-1L) Oro: svantaggio: un oggetto di connessione o una stringa di caratteri n: il numero massimo di righe da leggere. L&#8217;impostazione predefinita prevede la lettura di tutte [&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>Come utilizzare la funzione readLines() in R (con esempi) - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come utilizzare la funzione readLines() in R, con diversi esempi.\" \/>\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\/it\/leggi-le-righe-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come utilizzare la funzione readLines() in R (con esempi) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come utilizzare la funzione readLines() in R, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/leggi-le-righe-in-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=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/\",\"url\":\"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/\",\"name\":\"Come utilizzare la funzione readLines() in R (con esempi) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-19T19:08:39+00:00\",\"dateModified\":\"2023-07-19T19:08:39+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come utilizzare la funzione readLines() in R, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come utilizzare la funzione readlines() in r (con esempi)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come utilizzare la funzione readLines() in R (con esempi) - Statorials","description":"Questo tutorial spiega come utilizzare la funzione readLines() in R, con diversi esempi.","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\/it\/leggi-le-righe-in-r\/","og_locale":"it_IT","og_type":"article","og_title":"Come utilizzare la funzione readLines() in R (con esempi) - Statorials","og_description":"Questo tutorial spiega come utilizzare la funzione readLines() in R, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/leggi-le-righe-in-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":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/","url":"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/","name":"Come utilizzare la funzione readLines() in R (con esempi) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-19T19:08:39+00:00","dateModified":"2023-07-19T19:08:39+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come utilizzare la funzione readLines() in R, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/leggi-le-righe-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come utilizzare la funzione readlines() in r (con esempi)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2983"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=2983"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2983\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}