{"id":3619,"date":"2023-07-16T12:37:36","date_gmt":"2023-07-16T12:37:36","guid":{"rendered":"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/"},"modified":"2023-07-16T12:37:36","modified_gmt":"2023-07-16T12:37:36","slug":"tabella-dati-vs-frame-dati-in-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/","title":{"rendered":"Data.table vs data frame in r: tre differenze chiave"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Nel linguaggio di programmazione R, un <strong>data.frame<\/strong> fa parte del database R.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Qualsiasi <strong>data.frame<\/strong> pu\u00f2 essere convertito in <strong>data.table<\/strong> utilizzando la funzione <strong>setDF<\/strong> del pacchetto <strong>data.table<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Un data.table offre i seguenti vantaggi rispetto a un data.frame in R:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>1.<\/strong> \u00c8 possibile utilizzare la funzione <a href=\"https:\/\/statorials.org\/it\/ho-paura\/\" target=\"_blank\" rel=\"noopener\">fread<\/a> dal pacchetto data.table per leggere un file in un data.table <em>molto<\/em> pi\u00f9 velocemente delle funzioni R di base come <a href=\"https:\/\/statorials.org\/it\/importare-csv-in-r\/\" target=\"_blank\" rel=\"noopener\">read.csv<\/a> , che leggono i file in un data.frame.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>2.<\/strong> \u00c8 possibile eseguire operazioni (come raggruppamento e aggregazione) su un data.table <em>molto<\/em> pi\u00f9 velocemente di un data.frame.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>3.<\/strong> Quando si stampa un data.frame su una console, R tenter\u00e0 di stampare ogni riga nel data.frame. Tuttavia, un data.table mostrer\u00e0 solo le prime 100 righe, il che potrebbe impedire il blocco o l&#8217;arresto anomalo della sessione se stai lavorando con un set di dati di grandi dimensioni.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti illustrano nella pratica queste differenze tra data.frames e data.tables.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Differenza n. 1: importazione pi\u00f9 rapida con fread<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come importare un frame di dati di 10.000 righe e 100 colonne utilizzando la funzione <strong>fread<\/strong> dal pacchetto data.table e la funzione <strong>read.csv<\/strong> dal database R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (microbenchmark)\n<span style=\"color: #008000;\">library<\/span> (data.table)\n\n<span style=\"color: #008080;\">#make this example reproducible\n<\/span>set. <span style=\"color: #3366ff;\">seeds<\/span> (1)\n\n<span style=\"color: #008080;\">#create data frame with 10,000 rows and 100 columns\n<\/span>df &lt;- as. <span style=\"color: #3366ff;\">data<\/span> . <span style=\"color: #3366ff;\">frame<\/span> (matrix(runif(10^4 * 100), nrow = 10^4))\n\n<span style=\"color: #008080;\">#export CSV to current working directory\n<\/span>write.write. <span style=\"color: #3366ff;\">csv<\/span> (df, \" <span style=\"color: #ff0000;\">test.csv<\/span> \", quote = <span style=\"color: #008000;\">FALSE<\/span> )\n\n<span style=\"color: #008080;\">#import CSV file using fread and read.csv and time how long it takes\n<\/span>results &lt;- microbenchmark(\n  read.csv = read. <span style=\"color: #3366ff;\">csv<\/span> (\" <span style=\"color: #ff0000;\">test.csv<\/span> \", header = <span style=\"color: #008000;\">TRUE<\/span> , stringsAsFactors = <span style=\"color: #008000;\">FALSE<\/span> ),\n  fread = fread(\" <span style=\"color: #ff0000;\">test.csv<\/span> \", sep = \",\", stringsAsFactors = <span style=\"color: #008000;\">FALSE<\/span> ),\n  times = 10)\n\n<span style=\"color: #008080;\">#view results\n<\/span>results\n\nUnit: milliseconds\n     expr min lq mean median uq max neval cld\n read.csv 817.1867 892.8748 1026.7071 899.5755 926.9120 1964.0540 10 b\n    fread 113.5889 116.2735 136.4079 124.3816 136.0534 211.7484 10 a<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Dai risultati, possiamo vedere che <strong>fread<\/strong> \u00e8 circa 10 volte pi\u00f9 veloce nell&#8217;importare questo file CSV rispetto alla funzione <strong>read.csv<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tieni presente che questa differenza sar\u00e0 ancora maggiore per set di dati pi\u00f9 grandi.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Differenza n. 2: manipolazione dei dati pi\u00f9 rapida con data.table<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">In generale, <strong>data.table<\/strong> pu\u00f2 anche eseguire qualsiasi attivit\u00e0 di manipolazione dei dati molto pi\u00f9 velocemente di <strong>data.frame<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ad esempio, il codice seguente mostra come calcolare la media di una variabile, raggruppata da un&#8217;altra variabile sia in data.table che in data.frame:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">library<\/span> (microbenchmark)\n<span style=\"color: #008000;\">library<\/span> (data.table)\n\n<span style=\"color: #008080;\">#make this example reproducible\n<\/span>set.seed(1)\n\n#create data frame with 10,000 rows and 100 columns\nd_frame &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (team=rep(c(' <span style=\"color: #ff0000;\">A<\/span> ', ' <span style=\"color: #ff0000;\">B<\/span> '), each=5000),\n                      points=c(rnorm(10000, mean=20, sd=3)))\n\n<span style=\"color: #008080;\">#create data.table from data.frame\n<\/span>d_table &lt;- setDT(d_frame)\n\n<span style=\"color: #008080;\">#calculate mean of points grouped by team in data.frame and data.table\n<\/span>results &lt;- microbenchmark(\n  mean_d_frame = aggregate(d_frame$points, list(d_frame$team), FUN=mean),\n  mean_d_table = d_table[ ,list(mean=mean(points)), by=team],\n  times = 10)\n\n<span style=\"color: #008080;\">#view results\n<\/span>results\n\nUnit: milliseconds\n         expr min lq mean median uq max neval cld\n mean_d_frame 2.9045 3.0077 3.11683 3.1074 3.1654 3.4824 10 b\n mean_d_table 1.0539 1.1140 1.52002 1.2075 1.2786 3.6084 10 a<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Dai risultati, possiamo vedere che <strong>data.table<\/strong> \u00e8 circa tre volte pi\u00f9 veloce di <strong>data.frame<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Per set di dati pi\u00f9 grandi, questa differenza sar\u00e0 ancora maggiore.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Differenza n. 3: meno righe stampate con data.table<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Quando si stampa un <strong>data.frame<\/strong> su una console, R tenter\u00e0 di stampare ogni riga nel data.frame.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tuttavia, un <strong>data.table<\/strong> mostrer\u00e0 solo le prime 100 righe, il che potrebbe impedire il blocco o l&#8217;arresto anomalo della sessione se stai lavorando con un set di dati di grandi dimensioni.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ad esempio, nel codice seguente creiamo sia un data frame che una data.table di 200 righe.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Quando si stampa data.frame, R tenter\u00e0 di stampare ogni riga mentre stampando data.table verranno visualizzate solo le prime cinque righe e le ultime cinque righe:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (data.table)\n\n<span style=\"color: #008080;\">#make this example reproducible\n<\/span>set. <span style=\"color: #3366ff;\">seeds<\/span> (1)\n\n<span style=\"color: #008080;\">#create data frame\n<\/span>d_frame &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (x=rnorm(200),\n                      y=rnorm(200),\n                      z=rnorm(200))\n<span style=\"color: #008080;\">#view data frame\n<\/span>d_frame\n\n               X Y Z\n1 -0.055303118 1.54858564 -2.065337e-02\n2 0.354143920 0.36706204 -3.743962e-01\n3 -0.999823809 -1.57842544 4.392027e-01\n4 2.586214840 0.17383147 -2.081125e+00\n5 -1.917692199 -2.11487401 4.073522e-01\n6 0.039614766 2.21644236 1.869164e+00\n7 -1.942259548 0.81566443 4.740712e-01\n8 -0.424913746 1.01081030 4.996065e-01\n9 -1.753210825 -0.98893038 -6.290307e-01\n10 0.232382655 -1.25229873 -1.324883e+00\n11 0.027278832 0.44209325 -3.221920e-01\n...\n<span style=\"color: #008080;\">#create data table\n<\/span>d_table &lt;- setDT(d_frame)\n\n<span style=\"color: #008080;\">#view data table\n<\/span>d_table\n\n               X Y Z\n  1: -0.05530312 1.54858564 -0.02065337\n  2: 0.35414392 0.36706204 -0.37439617\n  3: -0.99982381 -1.57842544 0.43920275\n  4: 2.58621484 0.17383147 -2.08112491\n  5: -1.91769220 -2.11487401 0.40735218\n ---                                    \n196: -0.06196178 1.08164065 0.58609090\n197: 0.34160667 -0.01886703 1.61296255\n198: -0.38361957 -0.03890329 0.71377217\n199: -0.80719743 -0.89674205 -0.49615702\n200: -0.26502679 -0.15887435 -1.73781026<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo \u00e8 un vantaggio offerto <strong>da data.table<\/strong> rispetto <strong>a data.frame<\/strong> , soprattutto quando si lavora con set di dati di grandi dimensioni che non si desidera stampare accidentalmente sulla console.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre attivit\u00e0 comuni in R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/r-aggiungi-al-dataframe\/\" target=\"_blank\" rel=\"noopener\">Come aggiungere righe a un frame di dati in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/r-mantieni-le-colonne\/\" target=\"_blank\" rel=\"noopener\">Come preservare alcune colonne in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/dplyr-seleziona-colonne-numeriche\/\" target=\"_blank\" rel=\"noopener\">Come selezionare solo colonne numeriche in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nel linguaggio di programmazione R, un data.frame fa parte del database R. Qualsiasi data.frame pu\u00f2 essere convertito in data.table utilizzando la funzione setDF del pacchetto data.table . Un data.table offre i seguenti vantaggi rispetto a un data.frame in R: 1. \u00c8 possibile utilizzare la funzione fread dal pacchetto data.table per leggere un file in un [&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>data.table vs data frame in R: tre differenze chiave - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega le principali differenze tra data.tables e data frame in R, inclusi 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\/tabella-dati-vs-frame-dati-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"data.table vs data frame in R: tre differenze chiave - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega le principali differenze tra data.tables e data frame in R, inclusi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-16T12:37:36+00:00\" \/>\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=\"4 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/\",\"url\":\"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/\",\"name\":\"data.table vs data frame in R: tre differenze chiave - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-16T12:37:36+00:00\",\"dateModified\":\"2023-07-16T12:37:36+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega le principali differenze tra data.tables e data frame in R, inclusi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data.table vs data frame in r: tre differenze chiave\"}]},{\"@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":"data.table vs data frame in R: tre differenze chiave - Statorials","description":"Questo tutorial spiega le principali differenze tra data.tables e data frame in R, inclusi 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\/tabella-dati-vs-frame-dati-in-r\/","og_locale":"it_IT","og_type":"article","og_title":"data.table vs data frame in R: tre differenze chiave - Statorials","og_description":"Questo tutorial spiega le principali differenze tra data.tables e data frame in R, inclusi esempi.","og_url":"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/","og_site_name":"Statorials","article_published_time":"2023-07-16T12:37:36+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"4 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/","url":"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/","name":"data.table vs data frame in R: tre differenze chiave - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-16T12:37:36+00:00","dateModified":"2023-07-16T12:37:36+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega le principali differenze tra data.tables e data frame in R, inclusi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/tabella-dati-vs-frame-dati-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Data.table vs data frame in r: tre differenze chiave"}]},{"@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\/3619"}],"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=3619"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3619\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}