{"id":3634,"date":"2023-07-16T10:37:23","date_gmt":"2023-07-16T10:37:23","guid":{"rendered":"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/"},"modified":"2023-07-16T10:37:23","modified_gmt":"2023-07-16T10:37:23","slug":"r-controleer-of-het-bestand-bestaat","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/","title":{"rendered":"Controleren of een bestand in r bestaat (met voorbeelden)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">U kunt de volgende basissyntaxis gebruiken om te controleren of er een bestand bestaat in uw huidige werkmap in R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>file. <span style=\"color: #3366ff;\">exists<\/span> (' <span style=\"color: #ff0000;\">my_data.csv <span style=\"color: #000000;\">')<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Deze functie retourneert <strong>TRUE<\/strong> als het bestand bestaat of <strong>FALSE<\/strong> als het niet bestaat.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Je kunt ook een if else-instructie gebruiken om een bestand in R alleen te lezen als het bestaat:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>data &lt;- ' <span style=\"color: #ff0000;\">my_data.csv<\/span> '\n\nif(file. <span style=\"color: #3366ff;\">exists<\/span> (data)){<\/strong>\n<strong>df &lt;- read. <span style=\"color: #3366ff;\">csv<\/span> (data)<\/strong>\n<strong>} else {<\/strong>\n<strong><span style=\"color: #008000;\">print<\/span> (' <span style=\"color: #ff0000;\">Does not exist<\/span> ')<\/strong>\n<strong>}<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Het volgende voorbeeld laat zien hoe u deze functies in de praktijk kunt gebruiken.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Voorbeeld: controleer of het bestand bestaat in R<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Laten we zeggen dat mijn huidige <a href=\"https:\/\/statorials.org\/nl\/setwd-getwd-in-r\/\" target=\"_blank\" rel=\"noopener\">werkmap<\/a> in R een map is met de naam <strong>test_data<\/strong> met drie CSV-bestanden:<\/span><\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-29854 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cheque1.jpg\" alt=\"\" width=\"426\" height=\"356\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">Ik kan <strong>list.files()<\/strong> gebruiken om de namen van elk bestand in de werkmap weer te geven:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#display the names of every file in current working directory\n<\/span>list. <span style=\"color: #3366ff;\">files<\/span> ()\n[1] \"my_data.csv\" \"my_new_data.csv\" \"some_old_data.csv\"\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Ik kan <strong>file.exists()<\/strong> gebruiken om te controleren of een bepaald bestand in de huidige werkmap bestaat:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#check if file 'my_data.csv' exists in current working directory\n<\/span>file. <span style=\"color: #3366ff;\">exists<\/span> (' <span style=\"color: #ff0000;\">my_data.csv<\/span> ')\n\n[1] TRUE<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">De functie retourneert <strong>TRUE<\/strong> , wat ons vertelt dat het bestand &#8218;my_data.csv&#8216; inderdaad bestaat in de huidige werkmap.<\/span><\/p>\n<p> <span style=\"color: #000000;\">We kunnen dan de volgende <strong>if else-<\/strong> instructie gebruiken om een bestand alleen te importeren als het bestaat:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define file name<\/span>\ndata &lt;- ' <span style=\"color: #ff0000;\">my_data.csv<\/span> '\n\n<span style=\"color: #008080;\">#import file only if it exists<\/span>\nif(file. <span style=\"color: #3366ff;\">exists<\/span> (data)){<\/strong>\n<strong>df &lt;- read. <span style=\"color: #3366ff;\">csv<\/span> (data)<\/strong>\n<strong>} else {<\/strong>\n<strong><span style=\"color: #008000;\">print<\/span> (' <span style=\"color: #ff0000;\">Does not exist<\/span> ')<\/strong>\n<strong>}\n\n<span style=\"color: #008080;\">#view contents of CSV file<\/span>\ndf\n\n  team points assists\n1 to 14 4\n2 B 26 7\n3 C 29 8\n4 D 20 3\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Omdat het bestand bestaat, kunnen we het met succes importeren.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Stel echter dat we een bestand proberen te importeren dat niet bestaat:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define file name<\/span>\ndata &lt;- ' <span style=\"color: #ff0000;\">this_data.csv<\/span> '\n\n<span style=\"color: #008080;\">#import file only if it exists<\/span>\nif(file. <span style=\"color: #3366ff;\">exists<\/span> (data)){<\/strong>\n<strong>df &lt;- read. <span style=\"color: #3366ff;\">csv<\/span> (data)<\/strong>\n<strong>} else {<\/strong>\n<strong><span style=\"color: #008000;\">print<\/span> (' <span style=\"color: #ff0000;\">Does not exist<\/span> ')<\/strong>\n<strong>}\n\n[1] \u201cDoes not exist\u201d\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">We ontvangen het bericht &#8222;Bestaat niet&#8220;, waarin staat dat een bestand met de naam <strong>this_data.csv<\/strong> niet bestaat in de huidige werkmap.<\/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 functies in R kunt gebruiken:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/lees-zip-bestand-in-r\/\" target=\"_blank\" rel=\"noopener\">Zip-bestanden lezen in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/csv-importeren-in-r\/\" target=\"_blank\" rel=\"noopener\">CSV-bestanden importeren in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/excel-importeren-in-r\/\" target=\"_blank\" rel=\"noopener\">Excel-bestanden importeren in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/hernoem-het-bestand-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe bestanden te hernoemen in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>U kunt de volgende basissyntaxis gebruiken om te controleren of er een bestand bestaat in uw huidige werkmap in R: file. exists (&#8218; my_data.csv &#8218;) Deze functie retourneert TRUE als het bestand bestaat of FALSE als het niet bestaat. Je kunt ook een if else-instructie gebruiken om een bestand in R alleen te lezen als [&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-3634","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 u kunt controleren of een bestand bestaat in R (met voorbeelden) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kunt controleren of een bestand in een map in R bestaat.\" \/>\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-controleer-of-het-bestand-bestaat\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe u kunt controleren of een bestand bestaat in R (met voorbeelden) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kunt controleren of een bestand in een map in R bestaat.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-16T10:37:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cheque1.jpg\" \/>\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-controleer-of-het-bestand-bestaat\/\",\"url\":\"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/\",\"name\":\"Hoe u kunt controleren of een bestand bestaat in R (met voorbeelden) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-16T10:37:23+00:00\",\"dateModified\":\"2023-07-16T10:37:23+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kunt controleren of een bestand in een map in R bestaat.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Controleren of een bestand in r bestaat (met voorbeelden)\"}]},{\"@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 u kunt controleren of een bestand bestaat in R (met voorbeelden) \u2013 Statorials","description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kunt controleren of een bestand in een map in R bestaat.","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-controleer-of-het-bestand-bestaat\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe u kunt controleren of een bestand bestaat in R (met voorbeelden) \u2013 Statorials","og_description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kunt controleren of een bestand in een map in R bestaat.","og_url":"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/","og_site_name":"Statorials","article_published_time":"2023-07-16T10:37:23+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cheque1.jpg"}],"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-controleer-of-het-bestand-bestaat\/","url":"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/","name":"Hoe u kunt controleren of een bestand bestaat in R (met voorbeelden) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-16T10:37:23+00:00","dateModified":"2023-07-16T10:37:23+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kunt controleren of een bestand in een map in R bestaat.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/r-controleer-of-het-bestand-bestaat\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Controleren of een bestand in r bestaat (met voorbeelden)"}]},{"@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\/3634","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=3634"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3634\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=3634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=3634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=3634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}