{"id":2970,"date":"2023-07-19T20:47:38","date_gmt":"2023-07-19T20:47:38","guid":{"rendered":"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/"},"modified":"2023-07-19T20:47:38","modified_gmt":"2023-07-19T20:47:38","slug":"pivot_longer-naar-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/","title":{"rendered":"Hoe pivot_longer() te gebruiken in r"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">De functie <strong>pivot_longer()<\/strong> van het <a href=\"https:\/\/tidyr.tidyverse.org\/\" target=\"_blank\" rel=\"noopener\">Tidyr-<\/a> pakket in R kan worden gebruikt om een gegevensblok van een breed formaat naar een lang formaat te roteren.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Deze functie gebruikt de volgende basissyntaxis:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><b><span style=\"color: #008000;\">library<\/span> (tidyr)\n\n<strong>df %&gt;% pivot_longer(cols=c(' <span style=\"color: #ff0000;\">var1<\/span> ', ' <span style=\"color: #ff0000;\">var2<\/span> ', ...),\n                    names_to=' <span style=\"color: #ff0000;\">col1_name<\/span> ',\n                    values_to=' <span style=\"color: #ff0000;\">col2_name<\/span> ')<\/strong>\n<\/b><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Goud:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>cols<\/strong> : De namen van de kolommen die moeten draaien<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>namen_to<\/strong> : De naam van de nieuwe tekenkolom<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>waarden_to<\/strong> : De naam van de nieuwe waardenkolom<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Het volgende voorbeeld laat zien hoe u deze functie in de praktijk kunt gebruiken.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Gerelateerd:<\/strong> <a href=\"https:\/\/statorials.org\/nl\/lange-data-versus-brede-data\/\" target=\"_blank\" rel=\"noopener\">Lange of brede data: wat is het verschil?<\/a><\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Voorbeeld: Gebruik pivot_longer() in R<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Stel dat we het volgende gegevensframe in R hebben dat het aantal punten weergeeft dat door verschillende basketbalspelers in verschillende jaren is gescoord:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#create data frame\n<\/span>df &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (player=c('A', 'B', 'C', 'D'),\n                 year1=c(12, 15, 19, 19),\n                 year2=c(22, 29, 18, 12))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n  player year1 year2\n1 to 12 22\n2 B 15 29\n3 C 19 18\n4 D 19 12<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">We kunnen de functie <strong>pivot_longer()<\/strong> gebruiken om dit dataframe naar een lang formaat te roteren:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">library<\/span> (tidyr)\n\n<span style=\"color: #008080;\">#pivot the data frame into a long format\n<\/span>df %&gt;% pivot_longer(cols=c(' <span style=\"color: #ff0000;\">year1<\/span> ', ' <span style=\"color: #ff0000;\">year2<\/span> '),\n                    names_to=' <span style=\"color: #ff0000;\">year<\/span> ',\n                    values_to=' <span style=\"color: #ff0000;\">points<\/span> ')\n\n# A tibble: 8 x 3\n  player year points\n      \n1 A year1 12\n2 A year2 22\n3 B year1 15\n4 B year2 29\n5 C year1 19\n6 C year2 18\n7 D year1 19\n8 D year2 12\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Merk op dat de kolomnamen <strong>jaar1<\/strong> en <strong>jaar2<\/strong> nu worden gebruikt als waarden in een nieuwe kolom genaamd &#8222;jaar&#8220; en dat de waarden uit deze oorspronkelijke kolommen in een nieuwe kolom worden geplaatst met de naam &#8222;punten&#8220;.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Het eindresultaat is een lang dataframe.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Opmerking<\/strong> : u kunt de volledige documentatie voor de functie <strong>pivot_longer()<\/strong> <a href=\"https:\/\/tidyr.tidyverse.org\/reference\/pivot_longer.html\" target=\"_blank\" rel=\"noopener\">hier<\/a> vinden.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Aanvullende bronnen<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">In de volgende tutorials wordt uitgelegd hoe u andere veelvoorkomende functies uit het Tidyr-pakket in R kunt gebruiken:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/pivot_wider-r\/\" target=\"_blank\" rel=\"noopener\">Hoe pivot_wider() te gebruiken in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/spreidingsfunctie-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe de Spread-functie in R te gebruiken<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/verzamelfunctie-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe de collect-functie in R te gebruiken<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/aparte-functie-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe een aparte functie in R te gebruiken<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/verenig-functie-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe de Unite-functie in R te gebruiken<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>De functie pivot_longer() van het Tidyr- pakket in R kan worden gebruikt om een gegevensblok van een breed formaat naar een lang formaat te roteren. Deze functie gebruikt de volgende basissyntaxis: library (tidyr) df %&gt;% pivot_longer(cols=c(&#8218; var1 &#8218;, &#8218; var2 &#8218;, &#8230;), names_to=&#8216; col1_name &#8218;, values_to=&#8216; col2_name &#8218;) Goud: cols : De namen van de [&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-2970","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 pivot_longer() te gebruiken in R \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt met een voorbeeld uitgelegd hoe u de functie pivot_longer() uit het Tidyr-pakket in R gebruikt.\" \/>\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\/pivot_longer-naar-r\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe pivot_longer() te gebruiken in R \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt met een voorbeeld uitgelegd hoe u de functie pivot_longer() uit het Tidyr-pakket in R gebruikt.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-19T20:47:38+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\/pivot_longer-naar-r\/\",\"url\":\"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/\",\"name\":\"Hoe pivot_longer() te gebruiken in R \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-19T20:47:38+00:00\",\"dateModified\":\"2023-07-19T20:47:38+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt met een voorbeeld uitgelegd hoe u de functie pivot_longer() uit het Tidyr-pakket in R gebruikt.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoe pivot_longer() te gebruiken in r\"}]},{\"@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 pivot_longer() te gebruiken in R \u2013 Statorials","description":"In deze tutorial wordt met een voorbeeld uitgelegd hoe u de functie pivot_longer() uit het Tidyr-pakket in R gebruikt.","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\/pivot_longer-naar-r\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe pivot_longer() te gebruiken in R \u2013 Statorials","og_description":"In deze tutorial wordt met een voorbeeld uitgelegd hoe u de functie pivot_longer() uit het Tidyr-pakket in R gebruikt.","og_url":"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/","og_site_name":"Statorials","article_published_time":"2023-07-19T20:47:38+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\/pivot_longer-naar-r\/","url":"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/","name":"Hoe pivot_longer() te gebruiken in R \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-19T20:47:38+00:00","dateModified":"2023-07-19T20:47:38+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt met een voorbeeld uitgelegd hoe u de functie pivot_longer() uit het Tidyr-pakket in R gebruikt.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/pivot_longer-naar-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Hoe pivot_longer() te gebruiken in r"}]},{"@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\/2970","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=2970"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/2970\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=2970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=2970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=2970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}