{"id":3283,"date":"2023-07-18T07:35:38","date_gmt":"2023-07-18T07:35:38","guid":{"rendered":"https:\/\/statorials.org\/it\/dplyr-attraverso\/"},"modified":"2023-07-18T07:35:38","modified_gmt":"2023-07-18T07:35:38","slug":"dplyr-attraverso","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/dplyr-attraverso\/","title":{"rendered":"Come utilizzare la funzione across() in dplyr (3 esempi)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Puoi utilizzare la funzione <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/across.html\" target=\"_blank\" rel=\"noopener\">across()<\/a> dal pacchetto <a href=\"https:\/\/dplyr.tidyverse.org\/\" target=\"_blank\" rel=\"noopener\">dplyr<\/a> in R per applicare una trasformazione a pi\u00f9 colonne.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Esistono innumerevoli<\/span> <span style=\"color: #000000;\">modi per utilizzare questa funzionalit\u00e0, ma i seguenti metodi illustrano alcuni usi comuni:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: applicare una funzione a pi\u00f9 colonne<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#multiply values in col1 and col2 by 2<\/span>\ndf %&gt;% \n  mutate(across(c(col1, col2), <span style=\"color: #008000;\">function<\/span> (x) x*2))\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: calcolare una statistica riepilogativa per pi\u00f9 colonne<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#calculate mean of col1 and col2<\/span>\ndf %&gt;%\n  summarise(across(c(col1, col2), mean, na. <span style=\"color: #3366ff;\">rm<\/span> = <span style=\"color: #008000;\">TRUE<\/span> ))\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 3: calcolare pi\u00f9 statistiche di riepilogo per pi\u00f9 colonne<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#calculate mean and standard deviation for col1 and col2\n<\/span>df %&gt;%\n  summarise(across(c(col1, col2), list(mean=mean, sd=sd), na. <span style=\"color: #3366ff;\">rm<\/span> = <span style=\"color: #008000;\">TRUE<\/span> ))\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come utilizzare ciascun metodo 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> (conf=c('East', 'East', 'East', 'West', 'West', 'West'),\n                 points=c(22, 25, 29, 13, 22, 30),\n                 rebounds=c(12, 10, 6, 6, 8, 11))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n  conf points rebounds\n1 East 22 12\n2 East 25 10\n3 East 29 6\n4 West 13 6\n5 West 22 8\n6 West 30 11<\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 1: applicare una funzione a pi\u00f9 colonne<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <strong>across()<\/strong> per moltiplicare per 2 i valori nelle colonne <strong>points<\/strong> e <strong>rebounds<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">library<\/span> (dplyr)\n\n<span style=\"color: #008080;\">#multiply values in points and rebounds columns by 2\n<\/span>df %&gt;% \n  mutate(across(c(points, rebounds), <span style=\"color: #008000;\">function<\/span> (x) x*2))\n\n  conf points rebounds\n1 East 44 24\n2 East 50 20\n3 East 58 12\n4 West 26 12\n5 West 44 16\n6 West 60 22\n<\/strong><\/span><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 2: calcolare una statistica riepilogativa per pi\u00f9 colonne<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <strong>across()<\/strong> per calcolare il valore medio delle colonne <strong>punti<\/strong> e <strong>rimbalzi<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">library<\/span> (dplyr)<\/span>\n\n#calculate mean value of points an rebounds columns\n<\/span>df %&gt;%\n  summarise(across(c(points, rebounds), mean, na. <span style=\"color: #3366ff;\">rm<\/span> = <span style=\"color: #008000;\">TRUE<\/span> ))\n\n  rebound points\n1 23.5 8.833333<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che possiamo anche utilizzare la funzione <strong>is.numeric<\/strong> per calcolare automaticamente una statistica riepilogativa per tutte le colonne numeriche nel data frame:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">library<\/span> (dplyr)<\/span>\n\n#calculate mean value for every numeric column in data frame\n<\/span>df %&gt;%\n  summarise(across(where(is. <span style=\"color: #3366ff;\">numeric<\/span> ), mean, na. <span style=\"color: #3366ff;\">rm<\/span> = <span style=\"color: #008000;\">TRUE<\/span> ))\n\n  rebound points\n1 23.5 8.833333\n<\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 3: calcolare pi\u00f9 statistiche di riepilogo per pi\u00f9 colonne<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <strong>across()<\/strong> per calcolare la media e la deviazione standard delle colonne <strong>punti<\/strong> e <strong>rimbalzi<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008000;\">library<\/span> (dplyr)<\/span>\n\n#calculate mean and standard deviation for points and rebounds columns\n<\/span>df %&gt;%\n  summarise(across(c(points, rebounds), list(mean=mean, sd=sd), na. <span style=\"color: #3366ff;\">rm<\/span> = <span style=\"color: #008000;\">TRUE<\/span> ))\n\n  points_mean points_sd rebounds_mean rebounds_sd\n1 23.5 6.156298 8.833333 2.562551<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa per la funzione <strong>across()<\/strong> <a href=\"https:\/\/dplyr.tidyverse.org\/reference\/across.html\" target=\"_blank\" rel=\"noopener\">qui<\/a> .<\/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 funzioni comuni utilizzando dplyr:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/dplyr-cancella-righe\/\" target=\"_blank\" rel=\"noopener\">Come eliminare righe utilizzando dplyr<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/organizzare-le-linee-r\/\" target=\"_blank\" rel=\"noopener\">Come organizzare le righe utilizzando dplyr<\/a><br \/> Come filtrare in base a pi\u00f9 condizioni utilizzando dplyr<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Puoi utilizzare la funzione across() dal pacchetto dplyr in R per applicare una trasformazione a pi\u00f9 colonne. Esistono innumerevoli modi per utilizzare questa funzionalit\u00e0, ma i seguenti metodi illustrano alcuni usi comuni: Metodo 1: applicare una funzione a pi\u00f9 colonne #multiply values in col1 and col2 by 2 df %&gt;% mutate(across(c(col1, col2), function (x) x*2)) [&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 across() in dplyr (3 esempi) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come utilizzare la funzione across() in dplyr, 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\/dplyr-attraverso\/\" \/>\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 across() in dplyr (3 esempi) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come utilizzare la funzione across() in dplyr, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/dplyr-attraverso\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-18T07:35:38+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\/dplyr-attraverso\/\",\"url\":\"https:\/\/statorials.org\/it\/dplyr-attraverso\/\",\"name\":\"Come utilizzare la funzione across() in dplyr (3 esempi) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-18T07:35:38+00:00\",\"dateModified\":\"2023-07-18T07:35:38+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come utilizzare la funzione across() in dplyr, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/dplyr-attraverso\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/dplyr-attraverso\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/dplyr-attraverso\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come utilizzare la funzione across() in dplyr (3 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 across() in dplyr (3 esempi) \u2013 Statorials","description":"Questo tutorial spiega come utilizzare la funzione across() in dplyr, 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\/dplyr-attraverso\/","og_locale":"it_IT","og_type":"article","og_title":"Come utilizzare la funzione across() in dplyr (3 esempi) \u2013 Statorials","og_description":"Questo tutorial spiega come utilizzare la funzione across() in dplyr, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/dplyr-attraverso\/","og_site_name":"Statorials","article_published_time":"2023-07-18T07:35:38+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\/dplyr-attraverso\/","url":"https:\/\/statorials.org\/it\/dplyr-attraverso\/","name":"Come utilizzare la funzione across() in dplyr (3 esempi) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-18T07:35:38+00:00","dateModified":"2023-07-18T07:35:38+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come utilizzare la funzione across() in dplyr, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/dplyr-attraverso\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/dplyr-attraverso\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/dplyr-attraverso\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come utilizzare la funzione across() in dplyr (3 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\/3283"}],"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=3283"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3283\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}