{"id":3088,"date":"2023-07-19T06:57:10","date_gmt":"2023-07-19T06:57:10","guid":{"rendered":"https:\/\/statorials.org\/it\/allegare-nuovamente\/"},"modified":"2023-07-19T06:57:10","modified_gmt":"2023-07-19T06:57:10","slug":"allegare-nuovamente","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/allegare-nuovamente\/","title":{"rendered":"Come utilizzare attach() in r (con esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u00c8 possibile utilizzare la funzione <b>attach()<\/b> in R per rendere accessibili gli oggetti del frame di dati senza dover digitare il nome del frame di dati.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questa funzione utilizza la seguente sintassi di base:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>attach(data)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">I seguenti esempi mostrano come utilizzare questa funzione in diversi scenari con il seguente frame di dati:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><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'),\n                 points=c(99, 90, 86, 88, 95),\n                 assists=c(33, 28, 31, 39, 34),\n                 rebounds=c(30, 28, 24, 24, 28))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n  team points assists rebounds\n1 A 99 33 30\n2 B 90 28 28\n3 C 86 31 24\n4 D 88 39 24\n5 E 95 34 28\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: utilizzare attach() per eseguire calcoli<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Normalmente se vogliamo calcolare la media, la mediana, l&#8217;intervallo, ecc. di una colonna in un frame di dati, utilizzeremo la seguente sintassi:<\/span><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#calculate mean of rebounds column\n<\/span>mean(df$rebounds)\n\n[1] 26.8\n\n<span style=\"color: #008080;\">#calculate median of rebounds column<\/span>\nmedian(df$rebounds)\n\n[1] 28\n\n<span style=\"color: #008080;\">#calculate range of rebounds column<\/span>\nrange(df$rebounds)\n\n[1] 24 30\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tuttavia, se utilizziamo <strong>attach()<\/strong> , non abbiamo nemmeno bisogno di inserire il nome del frame di dati per eseguire questi calcoli:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\"><span style=\"color: #000000;\">attach(df)<\/span>\n\n#calculate mean of rebounds column\n<\/span>mean(rebounds)\n\n[1] 26.8\n<span style=\"color: #008080;\">#calculate median of rebounds column<\/span>\nmedian(rebounds)\n\n[1] 28\n<span style=\"color: #008080;\">#calculate range of rebounds column<\/span>\nrange(rebounds)\n\n[1] 24 30\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Usando <strong>attach()<\/strong> , possiamo fare riferimento direttamente al nome della colonna e R sa quale frame di dati stiamo cercando di utilizzare.<\/span><\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: utilizzare attach() per adattare i modelli di regressione<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Normalmente, se vogliamo adattare un modello di regressione lineare in R, utilizzeremo la seguente sintassi:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#fit regression model\n<\/span>fit &lt;- lm(points ~ assists + rebounds, data=df)\n\n<span style=\"color: #008080;\">#view coefficients of regression model\n<\/span>summary(fit)$coef\n\n              Estimate Std. Error t value Pr(&gt;|t|)\n(Intercept) 18.7071984 13.2030474 1.416885 0.29222633\nassists 0.5194553 0.2162095 2.402555 0.13821408\nrebounds 2.0802529 0.3273034 6.355733 0.02387244<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tuttavia, se utilizziamo <strong>attach()<\/strong> , non abbiamo nemmeno bisogno di utilizzare l&#8217;argomento <strong>data<\/strong> nella funzione <strong>lm()<\/strong> per adattarci al modello di regressione:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#fit regression model\n<\/span>fit &lt;- lm(points ~ assists + rebounds)\n\n<span style=\"color: #008080;\">#view coefficients of regression model\n<\/span>summary(fit)$coef\n\n              Estimate Std. Error t value Pr(&gt;|t|)\n(Intercept) 18.7071984 13.2030474 1.416885 0.29222633\nassists 0.5194553 0.2162095 2.402555 0.13821408\nrebounds 2.0802529 0.3273034 6.355733 0.02387244<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Si noti che i risultati della regressione sono esattamente gli stessi.<\/span><\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Bonus: usa detach() e search()<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">\u00c8 possibile utilizzare la funzione <strong>search()<\/strong> per visualizzare tutti gli oggetti allegati nell&#8217;ambiente R corrente:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#show all attached objects<\/span>\nsearch()\n\n [1] \".GlobalEnv\" \"df\" \"package:stats\"    \n [4] \"package:graphics\" \"package:grDevices\" \"package:utils\"    \n [7] \"package:datasets\" \"package:methods\" \"Autoloads\"        \n[10] \"package:base\"  \n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">E puoi usare la funzione <strong>detach()<\/strong> per staccare un oggetto attualmente scollegato:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#detach data frame<\/span>\ndetach(df)<\/span><\/span><\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\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\/ambiente-chiaro-in-r\/\" target=\"_blank\" rel=\"noopener\">Come cancellare l&#8217;ambiente in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/cancellare-le-tracce-nella-r\/\" target=\"_blank\" rel=\"noopener\">Come cancellare tutti i grafici in RStudio<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/r-stampa-piu-variabili\/\" target=\"_blank\" rel=\"noopener\">Come stampare pi\u00f9 variabili sulla stessa riga in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00c8 possibile utilizzare la funzione attach() in R per rendere accessibili gli oggetti del frame di dati senza dover digitare il nome del frame di dati. Questa funzione utilizza la seguente sintassi di base: attach(data) I seguenti esempi mostrano come utilizzare questa funzione in diversi scenari con il seguente frame di dati: #create data frame [&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 attach() in R (con esempi) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come utilizzare la funzione attach() 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\/allegare-nuovamente\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come utilizzare attach() in R (con esempi) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come utilizzare la funzione attach() in R, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/allegare-nuovamente\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-19T06:57:10+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=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/allegare-nuovamente\/\",\"url\":\"https:\/\/statorials.org\/it\/allegare-nuovamente\/\",\"name\":\"Come utilizzare attach() in R (con esempi) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-19T06:57:10+00:00\",\"dateModified\":\"2023-07-19T06:57:10+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come utilizzare la funzione attach() in R, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/allegare-nuovamente\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/allegare-nuovamente\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/allegare-nuovamente\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come utilizzare attach() 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 attach() in R (con esempi) \u2013 Statorials","description":"Questo tutorial spiega come utilizzare la funzione attach() 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\/allegare-nuovamente\/","og_locale":"it_IT","og_type":"article","og_title":"Come utilizzare attach() in R (con esempi) \u2013 Statorials","og_description":"Questo tutorial spiega come utilizzare la funzione attach() in R, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/allegare-nuovamente\/","og_site_name":"Statorials","article_published_time":"2023-07-19T06:57:10+00:00","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\/allegare-nuovamente\/","url":"https:\/\/statorials.org\/it\/allegare-nuovamente\/","name":"Come utilizzare attach() in R (con esempi) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-19T06:57:10+00:00","dateModified":"2023-07-19T06:57:10+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come utilizzare la funzione attach() in R, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/allegare-nuovamente\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/allegare-nuovamente\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/allegare-nuovamente\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come utilizzare attach() 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\/3088"}],"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=3088"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3088\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}