{"id":240,"date":"2013-06-26T10:24:27","date_gmt":"2013-06-26T14:24:27","guid":{"rendered":"http:\/\/www.clayford.net\/statistics\/?p=240"},"modified":"2013-06-26T10:24:27","modified_gmt":"2013-06-26T14:24:27","slug":"editing-a-lot-of-variable-names-in-a-r-data-frame","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/editing-a-lot-of-variable-names-in-a-r-data-frame\/","title":{"rendered":"Editing a lot of variable names in a R data frame"},"content":{"rendered":"<p>Someone I work with asked about how to easily update lots of variable names in a R data frame after importing a CSV file. Apparently the column headers in the CSV file are long and unwieldy, and simply opening the CSV file beforehand and editing the variable names is not desirable. So what to do? Well you\u2019re going to have to actually type the new variable names <em>at least once<\/em>. There\u2019s no getting around that. But it would be nice to type them only once and never again. This would be useful if you need to import the same CSV file over and over as it gets updated over time. Maybe it\u2019s a quarterly report or weekly data dump of some sort. In that case we can do something like the following:<\/p>\n<pre>\r\n# read in data set and save variable names into a vector\r\nds <- read.csv(\"dataset.csv\",header=TRUE)\r\nold_names <- names(ds)\r\n\r\n# write variable names to text file\r\nwriteLines(old_names, \"names.txt\")\r\n<\/pre>\n<p>We imported the CSV file, saved the variable names to a vector, and then wrote that vector to a text file with each variable on its own line. Now we can open the text file in an editor and edit the variable names. I find this easier than doing it in the context of R code. No need for quotes, functions, assignment operators, etc. When you\u2019re done, save and close your text file. Now you\u2019re ready to change your variable names:<\/p>\n<pre>\r\nnames(ds) <- readLines(\"names.txt\")\r\n<\/pre>\n<p>Done! The next time you need to import the same CSV file, you can just run these two lines:<\/p>\n<pre>\r\nds <- read.csv(\"dataset.csv\",header=TRUE)\r\nnames(ds) <- readLines(\"names.txt\")\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Someone I work with asked about how to easily update lots of variable names in a R data frame after&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/editing-a-lot-of-variable-names-in-a-r-data-frame\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-240","post","type-post","status-publish","format-standard","hentry","category-using-r"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=240"}],"version-history":[{"count":2,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":242,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/240\/revisions\/242"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}