{"id":3929,"date":"2023-07-14T17:12:50","date_gmt":"2023-07-14T17:12:50","guid":{"rendered":"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/"},"modified":"2023-07-14T17:12:50","modified_gmt":"2023-07-14T17:12:50","slug":"r-cumulatief-bedrag-per-groep","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/","title":{"rendered":"Hoe de cumulatieve som per groep in r te berekenen"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Je kunt de volgende methoden gebruiken om een cumulatief bedrag per groep in R te berekenen:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Methode 1: Gebruik Base R<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df$cum_sum &lt;- ave(df$values_var, df$group_var, FUN=cumsum)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Methode 2: gebruik dplyr<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (dplyr)\n\ndf %&gt;% group_by(group_var) %&gt;% mutate(cum_sum = cumsum(values_var))\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Methode 3: Gebruik data.table<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (data.table)\n\nsetDT(df)[, cum_sum := cumsum(values_var), group_var] \n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">De volgende voorbeelden laten zien hoe u elke methode in de praktijk kunt gebruiken met het volgende dataframe in R:<\/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> (store=rep(c(' <span style=\"color: #ff0000;\">A<\/span> ', ' <span style=\"color: #ff0000;\">B<\/span> ', ' <span style=\"color: #ff0000;\">C<\/span> '), each= <span style=\"color: #008000;\">4<\/span> ),\n                 sales=c(3, 4, 4, 2, 5, 8, 9, 7, 6, 8, 3, 2))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n   blind sales\n1 to 3\n2 to 4\n3 to 4\n4 to 2\n5 B 5\n6 B 8\n7 B 9\n8 B 7\n9 C 6\n10 C 8\n11 C 3\n12 C 2\n<\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld 1: bereken de cumulatieve som per groep met behulp van de R-basis<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u de functie <strong>ave()<\/strong> van de R-database gebruikt om de cumulatieve som van <strong>sales<\/strong> te berekenen, gegroepeerd per <strong>winkel<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#add column to show cumulative sales by store\n<\/span>df$cum_sales &lt;- ave(df$sales, df$store, FUN=cumsum)\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n   store sales cum_sales\n1 to 3 3\n2 to 4 7\n3 to 4 11\n4 to 2 13\n5 B 5 5\n6 B 8 13\n7 B 9 22\n8 B 7 29\n9 C 6 6\n10 C 8 14\n11 C 3 17\n12 C 2 19<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">De nieuwe kolom genaamd <strong>cum_sales<\/strong> geeft de cumulatieve som van <strong>de verkopen<\/strong> weer, gegroepeerd per <strong>winkel<\/strong> .<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld 2: Bereken de cumulatieve som per groep met behulp van dplyr<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u verschillende functies uit het dplyr-pakket in R kunt gebruiken om de cumulatieve som van <strong>sales<\/strong> te berekenen, gegroepeerd per <strong>winkel<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (dplyr)\n\n<span style=\"color: #008080;\">#add column to show cumulative sales by store\n<\/span>df %&gt;% group_by(store) %&gt;% mutate(cum_sales = cumsum(sales))\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n# A tibble: 12 x 3\n# Groups: store [3]\n   store sales cum_sales\n         \n 1 to 3 3\n 2 to 4 7\n 3 to 4 11\n 4 to 2 13\n 5 B 5 5\n 6 B 8 13\n 7 B 9 22\n 8 B 7 29\n 9 C 6 6\n10 C 8 14\n11 C 3 17\n12 C 2 19\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">De nieuwe kolom genaamd <strong>cum_sales<\/strong> geeft de cumulatieve som van <strong>de verkopen<\/strong> weer, gegroepeerd per <strong>winkel<\/strong> .<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld 3: Bereken de cumulatieve som per groep met behulp van data.table<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u verschillende functies uit het pakket data.table in R kunt gebruiken om de cumulatieve som van <strong>sales<\/strong> te berekenen, gegroepeerd per <strong>winkel<\/strong> :<\/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;\">#add column to show cumulative sales by store\n<\/span>setDT(df)[, cum_sales := cumsum(sales), store] \n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n    store sales cum_sales\n 1: A 3 3\n 2: A 4 7\n 3: A 4 11\n 4: A 2 13\n 5: B 5 5\n 6: B 8 13\n 7: B 9 22\n 8: B 7 29\n 9: C 6 6\n10: C 8 14\n11: C 3 17\n12: C 2 19<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">De nieuwe kolom genaamd <strong>cum_sales<\/strong> geeft de cumulatieve som van <strong>de verkopen<\/strong> weer, gegroepeerd per <strong>winkel<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Opmerking<\/strong> : alle drie de methoden leveren hetzelfde resultaat op. De methoden dplyr en data.table zullen echter doorgaans sneller zijn bij het werken met extreem grote dataframes.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Aanvullende bronnen<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">In de volgende tutorials wordt uitgelegd hoe u andere veelvoorkomende berekeningen in R kunt uitvoeren:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/som-per-groep-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe de som per groep in R te berekenen<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/r-gemiddelde-per-groep\/\" target=\"_blank\" rel=\"noopener\">Hoe het gemiddelde per groep in R te berekenen<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/standaardafwijking-per-groep-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe de standaardafwijking per groep in R te berekenen<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Je kunt de volgende methoden gebruiken om een cumulatief bedrag per groep in R te berekenen: Methode 1: Gebruik Base R df$cum_sum &lt;- ave(df$values_var, df$group_var, FUN=cumsum) Methode 2: gebruik dplyr library (dplyr) df %&gt;% group_by(group_var) %&gt;% mutate(cum_sum = cumsum(values_var)) Methode 3: Gebruik data.table library (data.table) setDT(df)[, cum_sum := cumsum(values_var), group_var] De volgende voorbeelden laten zien [&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":[],"class_list":["post-3929","post","type-post","status-publish","format-standard","hentry","category-gids"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Hoe de cumulatieve som per groep te berekenen in R - Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt aan de hand van diverse voorbeelden uitgelegd hoe je een cumulatief bedrag per groep in R kunt berekenen.\" \/>\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\/nl\/r-cumulatief-bedrag-per-groep\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe de cumulatieve som per groep te berekenen in R - Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt aan de hand van diverse voorbeelden uitgelegd hoe je een cumulatief bedrag per groep in R kunt berekenen.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-14T17:12:50+00:00\" \/>\n<meta name=\"author\" content=\"Dr.benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Verfasst von\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr.benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"2\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/\",\"url\":\"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/\",\"name\":\"Hoe de cumulatieve som per groep te berekenen in R - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-14T17:12:50+00:00\",\"dateModified\":\"2023-07-14T17:12:50+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt aan de hand van diverse voorbeelden uitgelegd hoe je een cumulatief bedrag per groep in R kunt berekenen.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoe de cumulatieve som per groep in r te berekenen\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/nl\/#website\",\"url\":\"https:\/\/statorials.org\/nl\/\",\"name\":\"Statorials\",\"description\":\"Uw gids voor statistische competentie\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/nl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"de\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\",\"name\":\"Dr.benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr.benjamin anderson\"},\"description\":\"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder\",\"sameAs\":[\"http:\/\/statorials.org\/nl\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hoe de cumulatieve som per groep te berekenen in R - Statorials","description":"In deze tutorial wordt aan de hand van diverse voorbeelden uitgelegd hoe je een cumulatief bedrag per groep in R kunt berekenen.","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\/nl\/r-cumulatief-bedrag-per-groep\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe de cumulatieve som per groep te berekenen in R - Statorials","og_description":"In deze tutorial wordt aan de hand van diverse voorbeelden uitgelegd hoe je een cumulatief bedrag per groep in R kunt berekenen.","og_url":"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/","og_site_name":"Statorials","article_published_time":"2023-07-14T17:12:50+00:00","author":"Dr.benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr.benjamin anderson","Gesch\u00e4tzte Lesezeit":"2\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/","url":"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/","name":"Hoe de cumulatieve som per groep te berekenen in R - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-14T17:12:50+00:00","dateModified":"2023-07-14T17:12:50+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt aan de hand van diverse voorbeelden uitgelegd hoe je een cumulatief bedrag per groep in R kunt berekenen.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/r-cumulatief-bedrag-per-groep\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Hoe de cumulatieve som per groep in r te berekenen"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/nl\/#website","url":"https:\/\/statorials.org\/nl\/","name":"Statorials","description":"Uw gids voor statistische competentie","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/nl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"de"},{"@type":"Person","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219","name":"Dr.benjamin anderson","image":{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/","url":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr.benjamin anderson"},"description":"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder","sameAs":["http:\/\/statorials.org\/nl"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/comments?post=3929"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3929\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=3929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=3929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=3929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}