{"id":3289,"date":"2023-07-18T06:51:50","date_gmt":"2023-07-18T06:51:50","guid":{"rendered":"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/"},"modified":"2023-07-18T06:51:50","modified_gmt":"2023-07-18T06:51:50","slug":"r-functiespecifieke-dataframekolommen-toepassen","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/","title":{"rendered":"A: hoe u de functie apply() op specifieke kolommen gebruikt"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\"><span style=\"color: #000000;\">Vaak wilt u misschien de functie <strong>apply()<\/strong> gebruiken om een functie toe te passen op specifieke kolommen in een dataframe in R.<\/span><\/span><\/p>\n<p> <span style=\"color: #000000;\">De functie <strong>apply()<\/strong> dwingt echter eerst alle kolommen in een dataframe om hetzelfde objecttype te hebben voordat een functie wordt toegepast, wat soms onbedoelde gevolgen kan hebben.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Een betere keuze is de functie <strong>lapply()<\/strong> , die de volgende basissyntaxis gebruikt:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>df[c(' <span style=\"color: #ff0000;\">col1<\/span> ', ' <span style=\"color: #ff0000;\">col2<\/span> ')] &lt;- lapply(df[c(' <span style=\"color: #ff0000;\">col1<\/span> ', ' <span style=\"color: #ff0000;\">col2<\/span> ')], my_function)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">In dit specifieke voorbeeld wordt de functie <strong>my_function<\/strong> alleen toegepast op <strong>col1<\/strong> en <strong>col2<\/strong> in het dataframe.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Het volgende voorbeeld laat zien hoe u deze syntaxis in de praktijk kunt gebruiken.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld: pas een functie toe op specifieke kolommen in het dataframe<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Stel dat we het volgende dataframe in R hebben:<\/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', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),\n                 points=c(19, 22, 15, NA, 14, 25, 25, 25),\n                 rebounds=c(10, 6, 3, 7, 11, 13, 9, 12),\n                 assists=c(4, 4, 3, 6, 7, 5, 10, 8))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n  team points rebound assists\n1 A 19 10 4\n2 A 22 6 4\n3 to 15 3 3\n4 A NA 7 6\n5 B 14 11 7\n6 B 25 13 5\n7 B 25 9 10\n8 B 25 12 8<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Stel nu dat we de volgende functie defini\u00ebren die de waarden met 2 vermenigvuldigt en vervolgens 1 optelt:<\/span><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#define function\n<span style=\"color: #000000;\">my_function &lt;- <span style=\"color: #008000;\">function<\/span> (x) x*2 + 1<\/span><\/span><\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">We kunnen de volgende functie <strong>lapply()<\/strong> gebruiken om deze functie alleen toe te passen op de <strong>punten-<\/strong> en <strong>bouncekolommen<\/strong> in het dataframe:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#apply function to specific columns\n<\/span>df[c(' <span style=\"color: #ff0000;\">points<\/span> ', ' <span style=\"color: #ff0000;\">rebounds<\/span> ')] &lt;- lapply(df[c(' <span style=\"color: #ff0000;\">points<\/span> ', ' <span style=\"color: #ff0000;\">rebounds<\/span> ')], my_function)\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n  team points rebound assists\n1 A 39 21 4\n2 A 45 13 4\n3 A 31 7 3\n4 A NA 15 6\n5 B 29 23 7\n6 B 51 27 5\n7 B 51 19 10\n8 B 51 25 8\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Uit het resultaat kunnen we zien dat we elke waarde in de kolommen <strong>punten<\/strong> en <strong>rebounds<\/strong> met 2 vermenigvuldigen en vervolgens 1 optellen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Merk ook op dat de kolommen <strong>teams<\/strong> en <strong>assists<\/strong> ongewijzigd zijn gebleven.<\/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 taken in R kunt uitvoeren:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/een-gids-voor-het-aanbrengen-van-lapply-sapply-en-tapply-in-r\/\" target=\"_blank\" rel=\"noopener\">Een gids voor apply(), lapply(), sapply() en tapply() in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/transformeer-de-functie-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe de transformatiefunctie in R te gebruiken<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vaak wilt u misschien de functie apply() gebruiken om een functie toe te passen op specifieke kolommen in een dataframe in R. De functie apply() dwingt echter eerst alle kolommen in een dataframe om hetzelfde objecttype te hebben voordat een functie wordt toegepast, wat soms onbedoelde gevolgen kan hebben. Een betere keuze is de functie [&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-3289","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>A: Hoe de functie apply() op specifieke kolommen te gebruiken \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"In deze zelfstudie wordt uitgelegd hoe u de functie apply() alleen op specifieke dataframekolommen in R kunt gebruiken, inclusief voorbeelden.\" \/>\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-functiespecifieke-dataframekolommen-toepassen\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A: Hoe de functie apply() op specifieke kolommen te gebruiken \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze zelfstudie wordt uitgelegd hoe u de functie apply() alleen op specifieke dataframekolommen in R kunt gebruiken, inclusief voorbeelden.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-18T06:51: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-functiespecifieke-dataframekolommen-toepassen\/\",\"url\":\"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/\",\"name\":\"A: Hoe de functie apply() op specifieke kolommen te gebruiken \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-18T06:51:50+00:00\",\"dateModified\":\"2023-07-18T06:51:50+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze zelfstudie wordt uitgelegd hoe u de functie apply() alleen op specifieke dataframekolommen in R kunt gebruiken, inclusief voorbeelden.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A: hoe u de functie apply() op specifieke kolommen gebruikt\"}]},{\"@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":"A: Hoe de functie apply() op specifieke kolommen te gebruiken \u2013 Statorials","description":"In deze zelfstudie wordt uitgelegd hoe u de functie apply() alleen op specifieke dataframekolommen in R kunt gebruiken, inclusief voorbeelden.","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-functiespecifieke-dataframekolommen-toepassen\/","og_locale":"de_DE","og_type":"article","og_title":"A: Hoe de functie apply() op specifieke kolommen te gebruiken \u2013 Statorials","og_description":"In deze zelfstudie wordt uitgelegd hoe u de functie apply() alleen op specifieke dataframekolommen in R kunt gebruiken, inclusief voorbeelden.","og_url":"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/","og_site_name":"Statorials","article_published_time":"2023-07-18T06:51: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-functiespecifieke-dataframekolommen-toepassen\/","url":"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/","name":"A: Hoe de functie apply() op specifieke kolommen te gebruiken \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-18T06:51:50+00:00","dateModified":"2023-07-18T06:51:50+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze zelfstudie wordt uitgelegd hoe u de functie apply() alleen op specifieke dataframekolommen in R kunt gebruiken, inclusief voorbeelden.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/r-functiespecifieke-dataframekolommen-toepassen\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"A: hoe u de functie apply() op specifieke kolommen gebruikt"}]},{"@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\/3289","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=3289"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3289\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=3289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=3289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=3289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}