{"id":12213,"date":"2022-09-30T01:47:18","date_gmt":"2022-09-29T20:17:18","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/"},"modified":"2022-09-30T01:47:18","modified_gmt":"2022-09-29T20:17:18","slug":"solved-combining-csv-files-in-r-to-different-columns-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/","title":{"rendered":"[Solved] Combining csv files in R to different columns [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-40756046\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"40756046\" data-parentid=\"40753139\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Here&#8217;s an option. It&#8217;s more or less handcrafted as I&#8217;m not aware of any single command to do this exactly as you&#8217;ve specified.<\/p>\n<pre class=\"lang-r prettyprint-override\"><code># the working directory contains d1.csv, d2.csv, d3.csv with\n# matching first two columns. The third column is random data\nread.csv(\".\/d1.csv\")\n#&gt;     a b         c\n#&gt; 1   1 A 0.5526777\n#&gt; 2   2 B 0.2161643\n#&gt; 3   3 C 0.3311132\n#&gt; 4   4 D 0.3577971\n#&gt; 5   5 E 0.2298579\n#&gt; 6   6 F 0.4014883\n#&gt; 7   7 G 0.2789038\n#&gt; 8   8 H 0.5729675\n#&gt; 9   9 I 0.3413949\n#&gt; 10 10 J 0.5807167\n\n## identify the .csv files in the working directory\nfile_list &lt;- dir(\".\", pattern = \"*.csv\")\nfile_list\n#&gt; [1] \"d1.csv\" \"d2.csv\" \"d3.csv\"\n\n## for each of the .csv files, extract the base filename and \n## create a new object with that name containing the data.\n## Additionally, name the third column by the basename\nfor ( file in file_list) {\n  f &lt;- sub(\"(.*)\\\\.csv\", \"\\\\1\", file)\n  assign(f, read.csv(file = file))\n  assign(f, setNames(get(f), c(names(get(f))[1:2], file)))\n}\n\n## at this point, the objects d1, d2, and d3 have been created, \n## containing their respective data. The third column of each of \n## these is their originating filename.\nd1\n#&gt;     a b    d1.csv\n#&gt; 1   1 A 0.5526777\n#&gt; 2   2 B 0.2161643\n#&gt; 3   3 C 0.3311132\n#&gt; 4   4 D 0.3577971\n#&gt; 5   5 E 0.2298579\n#&gt; 6   6 F 0.4014883\n#&gt; 7   7 G 0.2789038\n#&gt; 8   8 H 0.5729675\n#&gt; 9   9 I 0.3413949\n#&gt; 10 10 J 0.5807167\n\n## specify the names of the date and time columns (common between files)\ndate_col &lt;- \"a\"\ntime_col &lt;- \"b\"\n\n## use Reduce to go through the list of created objects and \n## merge them together\nlist_of_objects &lt;- mget(sub(\"(.*)\\\\.csv\", \"\\\\1\", file_list))\ncombined_files &lt;- Reduce(function(x, y) merge(x, y, by = c(date_col, time_col)), list_of_objects)\n\ncombined_files\n#&gt;     a b    d1.csv    d2.csv    d3.csv\n#&gt; 1  10 J 0.5807167 0.8181820 0.7073864\n#&gt; 2   1 A 0.5526777 0.3225574 0.3758595\n#&gt; 3   2 B 0.2161643 0.6933108 0.5654979\n#&gt; 4   3 C 0.3311132 0.9309869 0.1727413\n#&gt; 5   4 D 0.3577971 0.8810876 0.7802144\n#&gt; 6   5 E 0.2298579 0.1023579 0.9925649\n#&gt; 7   6 F 0.4014883 0.1328283 0.7610007\n#&gt; 8   7 G 0.2789038 0.2926512 0.7469455\n#&gt; 9   8 H 0.5729675 0.8727978 0.3073394\n#&gt; 10  9 I 0.3413949 0.3107775 0.4778286\n<\/code><\/pre>\n<p>If there&#8217;s a particular aspect that you don&#8217;t understand, let me know and I&#8217;ll expand the comments.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Combining csv files in R to different columns [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Here&#8217;s an option. It&#8217;s more or less handcrafted as I&#8217;m not aware of any single command to do this exactly as you&#8217;ve specified. # the working directory contains d1.csv, d2.csv, d3.csv with # matching first two columns. The third column is random data read.csv(&#8220;.\/d1.csv&#8221;) #&gt; a b c #&gt; 1 1 A 0.5526777 #&gt; &#8230; <a title=\"[Solved] Combining csv files in R to different columns [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/\" aria-label=\"More on [Solved] Combining csv files in R to different columns [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[483,321],"class_list":["post-12213","post","type-post","status-publish","format-standard","hentry","category-solved","tag-csv","tag-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Combining csv files in R to different columns [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Combining csv files in R to different columns [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Here&#8217;s an option. It&#8217;s more or less handcrafted as I&#8217;m not aware of any single command to do this exactly as you&#8217;ve specified. # the working directory contains d1.csv, d2.csv, d3.csv with # matching first two columns. The third column is random data read.csv(&quot;.\/d1.csv&quot;) #&gt; a b c #&gt; 1 1 A 0.5526777 #&gt; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-29T20:17:18+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Combining csv files in R to different columns [closed]\",\"datePublished\":\"2022-09-29T20:17:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/\"},\"wordCount\":69,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"csv\",\"r\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/\",\"name\":\"[Solved] Combining csv files in R to different columns [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-29T20:17:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Combining csv files in R to different columns [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Combining csv files in R to different columns [closed] - JassWeb","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:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Combining csv files in R to different columns [closed] - JassWeb","og_description":"[ad_1] Here&#8217;s an option. It&#8217;s more or less handcrafted as I&#8217;m not aware of any single command to do this exactly as you&#8217;ve specified. # the working directory contains d1.csv, d2.csv, d3.csv with # matching first two columns. The third column is random data read.csv(\".\/d1.csv\") #&gt; a b c #&gt; 1 1 A 0.5526777 #&gt; ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/","og_site_name":"JassWeb","article_published_time":"2022-09-29T20:17:18+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Combining csv files in R to different columns [closed]","datePublished":"2022-09-29T20:17:18+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/"},"wordCount":69,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["csv","r"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/","name":"[Solved] Combining csv files in R to different columns [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-29T20:17:18+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-combining-csv-files-in-r-to-different-columns-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Combining csv files in R to different columns [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/12213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=12213"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/12213\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=12213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=12213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=12213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}