{"id":3061,"date":"2023-07-19T10:02:18","date_gmt":"2023-07-19T10:02:18","guid":{"rendered":"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/"},"modified":"2023-07-19T10:02:18","modified_gmt":"2023-07-19T10:02:18","slug":"r-foutvervanging-bij-1-regelgegevens-bij-0","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/","title":{"rendered":"Hoe te repareren in r: vervanging heeft x-rijen, gegevens hebben y"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Een foutmelding die u kunt tegenkomen bij het gebruik van R is:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>Error in `$&lt;-.data.frame`(`*tmp*`, conf_full, value = c(\"West\", \"West\",: \n  replacement has 3 rows, data has 5\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Deze fout treedt op wanneer u probeert een nieuwe kolom toe te voegen aan een dataframe waarvan de waarden zijn gebaseerd op een bestaande kolom, maar u niet eerst de nieuwe kolom kunt maken.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Het volgende voorbeeld laat zien hoe u deze fout in de praktijk kunt oplossen.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Hoe de fout te reproduceren<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Stel dat we het volgende dataframe in R cre\u00ebren:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#create data frame\n<\/span>df &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (conference=c('W', 'W', 'W', 'E', 'E'),\n                 points=c(99, 90, 86, 88, 95),\n                 assists=c(33, 28, 31, 39, 34))\t\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n  conference points assists\n1 W 99 33\n2 W 90 28\n3 W 86 31\n4 E 88 39\n5 E 95 34<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Stel nu dat we proberen een nieuwe kolom toe te voegen aan het dataframe met de naam <strong>conf_full<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#attempt to create new column based on conference name\n<\/span>df$conf_full[which(df$conference==' <span style=\"color: #ff0000;\">W<\/span> ')] &lt;- ' <span style=\"color: #ff0000;\">West<\/span> '\ndf$conf_full[which(df$conference==' <span style=\"color: #ff0000;\">E<\/span> ')] &lt;- ' <span style=\"color: #ff0000;\">East<\/span> '\n\nError in `$&lt;-.data.frame`(`*tmp*`, conf_full, value = c(\"West\", \"West\",: \n  replacement has 3 rows, data has 5<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">We krijgen een foutmelding omdat de variabelenaam <strong>conf_full<\/strong> nog niet bestaat, wat betekent dat we nog geen waarden aan deze kolom kunnen toekennen.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Hoe u de fout kunt vermijden<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Om deze fout te voorkomen, kunnen we eerst de <strong>conf_full<\/strong> variabele maken en deze eenvoudigweg de waarden van NA toewijzen:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create conf_full variable<\/span>\ndf$conf_full &lt;- NA<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Nu de variabele bestaat, kunnen we er waarden aan toekennen:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#create new column based on conference\n<\/span>df$conf_full[which(df$conference==' <span style=\"color: #ff0000;\">W<\/span> ')] &lt;- ' <span style=\"color: #ff0000;\">West<\/span> '\ndf$conf_full[which(df$conference==' <span style=\"color: #ff0000;\">E<\/span> ')] &lt;- ' <span style=\"color: #ff0000;\">East<\/span> '\n\n<span style=\"color: #008080;\">#view updated data frame\n<\/span>df\n\n  conference points assists conf_full\n1 W 99 33 West\n2 W 90 28 West\n3 W 86 31 West\n4 E 88 39 East\n5 E 95 34 East<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Houd er rekening mee dat we deze keer geen fouten ontvangen omdat we eerst de variabele <strong>conf_full<\/strong> hebben gemaakt voordat we probeerden er waarden aan toe te wijzen.<\/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 fouten in R kunt oplossen:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/r-argumenten-impliceren-een-ander-aantal-regels\/\" target=\"_blank\" rel=\"noopener\">Hoe op te lossen in R: argumenten hebben betrekking op een verschillend aantal regels<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/dplyr-fout-bij-het-selecteren-van-ongebruikte-argumenten\/\" target=\"_blank\" rel=\"noopener\">Hoe op te lossen in R: fout bij het selecteren van ongebruikte argumenten<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/r-vervanging-op-lengte-nul\/\" target=\"_blank\" rel=\"noopener\">Hoe te repareren in R: vervanging heeft een lengte van nul<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Een foutmelding die u kunt tegenkomen bij het gebruik van R is: Error in `$&lt;-.data.frame`(`*tmp*`, conf_full, value = c(&#8222;West&#8220;, &#8222;West&#8220;,: replacement has 3 rows, data has 5 Deze fout treedt op wanneer u probeert een nieuwe kolom toe te voegen aan een dataframe waarvan de waarden zijn gebaseerd op een bestaande kolom, maar u niet [&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-3061","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 te repareren in R: vervanging heeft X-rijen, gegevens hebben Y - Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt uitgelegd hoe u de volgende fout in R kunt oplossen: vervanging heeft 1 rij, gegevens hebben 0.\" \/>\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-foutvervanging-bij-1-regelgegevens-bij-0\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe te repareren in R: vervanging heeft X-rijen, gegevens hebben Y - Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt uitgelegd hoe u de volgende fout in R kunt oplossen: vervanging heeft 1 rij, gegevens hebben 0.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-19T10:02:18+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-foutvervanging-bij-1-regelgegevens-bij-0\/\",\"url\":\"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/\",\"name\":\"Hoe te repareren in R: vervanging heeft X-rijen, gegevens hebben Y - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-19T10:02:18+00:00\",\"dateModified\":\"2023-07-19T10:02:18+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt uitgelegd hoe u de volgende fout in R kunt oplossen: vervanging heeft 1 rij, gegevens hebben 0.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoe te repareren in r: vervanging heeft x-rijen, gegevens hebben y\"}]},{\"@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 te repareren in R: vervanging heeft X-rijen, gegevens hebben Y - Statorials","description":"In deze tutorial wordt uitgelegd hoe u de volgende fout in R kunt oplossen: vervanging heeft 1 rij, gegevens hebben 0.","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-foutvervanging-bij-1-regelgegevens-bij-0\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe te repareren in R: vervanging heeft X-rijen, gegevens hebben Y - Statorials","og_description":"In deze tutorial wordt uitgelegd hoe u de volgende fout in R kunt oplossen: vervanging heeft 1 rij, gegevens hebben 0.","og_url":"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/","og_site_name":"Statorials","article_published_time":"2023-07-19T10:02:18+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-foutvervanging-bij-1-regelgegevens-bij-0\/","url":"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/","name":"Hoe te repareren in R: vervanging heeft X-rijen, gegevens hebben Y - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-19T10:02:18+00:00","dateModified":"2023-07-19T10:02:18+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt uitgelegd hoe u de volgende fout in R kunt oplossen: vervanging heeft 1 rij, gegevens hebben 0.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/r-foutvervanging-bij-1-regelgegevens-bij-0\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Hoe te repareren in r: vervanging heeft x-rijen, gegevens hebben y"}]},{"@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\/3061","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=3061"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3061\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=3061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=3061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=3061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}