{"id":1265,"date":"2015-08-10T20:56:08","date_gmt":"2015-08-10T19:56:08","guid":{"rendered":"http:\/\/pzd.hmy.temporary.site\/?p=1265"},"modified":"2017-10-30T15:35:03","modified_gmt":"2017-10-30T19:35:03","slug":"get-your-data-into-r","status":"publish","type":"post","link":"https:\/\/datascienceplus.com\/get-your-data-into-r\/","title":{"rendered":"Get Your Data into R: Import Data from SPSS, Stata, SAS, CSV or TXT"},"content":{"rendered":"<p>In this post we will show how to import data from other sources into the R workspace.<\/p>\n<h2>Import data from SPSS<\/h2>\n<p>R can import datasets from SPSS with the function <code>read.spss()<\/code> from the package <code>foreign<\/code>. Alternatively, the function <code>spss.get()<\/code> from <code>Hmisc<\/code> package can be used. While <code>foreign<\/code> is a default package in R, the <code>Hmisc<\/code> package need to be installed.<\/p>\n<p>Here is an example:<\/p>\n<pre>\r\nlibrary(foreign)\r\ndf <- read.spss(\"dataset.sav\", use.value.label=TRUE, to.data.frame=TRUE)\r\n<\/pre>\n<p><code>df<\/code> is the name of data frame I created in R, and <code>dataset.sav<\/code> is the file name of SPSS dataset we want to import, and <code>use.value.label=TRUE<\/code> to convert variables with value labels in SPSS into R factors, and <code>to.data.frame=TRUE<\/code> to make as data frame.<\/p>\n<h2>Import data from Stata<\/h2>\n<p>To import a dataset from Stata into R, the function <code>read.dta()<\/code> from <code>foreign<\/code> package is used. More specifically look the code below:<\/p>\n<pre>\r\nlibrary(foreign)\r\ndf <- read.dta(\"dataset.dta\")\r\n<\/pre>\n<p><code>df<\/code> is the name of data frame in R, and <code>dataset.dta<\/code> is the file name of Stata dataset we want to import.<\/p>\n<h2>Import data from SAS<\/h2>\n<p>To import a dataset from <code>SAS<\/code> into <code>R<\/code> there are different methods, but most recommended is to export first the dataset from <code>SAS<\/code> into <code>CSV<\/code> and then to import in <code>R<\/code>.<\/p>\n<p>First use the code below in SAS (not R) to export data:<\/p>\n<pre>\r\n# run in SAS\r\nproc export data=dataset\r\noutfile=\"datast.csv\"\r\ndbms=csv;\r\nrun;\r\n<\/pre>\n<p>Now that your data is exported you can import in R by using the code below:<\/p>\n<pre>\r\ndf <- read.csv(\"dataset.csv\",header=T,as.is=T)\r\n<\/pre>\n<p>Another way to upload SAS files (<code>.XPT<\/code>) direct to R environment is by using <code>Hmisc<\/code> package.<\/p>\n<pre>\r\nlibrary(Hmisc)\r\ndf <- sasxport.get(\"\/filename.xpt\") \r\n<\/pre>\n<h2>Import data from CSV<\/h2>\n<p>You can import data from <code>csv<\/code> into <code>R<\/code> by using <code>read.table()<\/code> function similarly as we used with importing <code>txt<\/code> files. Basically, this function reads a <code>csv<\/code> file in table format and saves it as a data frame.<\/p>\n<p>This is the code we use to import <code>csv<\/code> file into R<\/p>\n<pre>\r\ndf <- read.table(\"dataset.csv\", header=TRUE, sep=\",\")\r\n<\/pre>\n<p>Often I use the function <code>read.csv()<\/code> to import csv file into R<\/p>\n<pre>\r\ndf <- read.csv(\"dataset.csv\",header=T,as.is=T)\r\n<\/pre>\n<h2>Import data from TXT<\/h2>\n<p>To import text file into R use the function <code>read.table()<\/code>. This function reads a file in table format and saves it as a data frame. The code used to import text file is in example below.<\/p>\n<pre>\r\ndf <- read.table(\"dataset.txt\", as.is=TRUE, header=T)\r\n<\/pre>\n<p>In some cases I find this code useful for importing the data:<\/p>\n<pre>\r\ndf <- read.table(\"dataset.txt\", header=TRUE, sep=\",\")\r\n<\/pre>\n<p>Or this code:<\/p>\n<pre>\r\ndf <- read.table(\"dataset.txt\", header=T, strings=F)\r\n<\/pre>\n<h2>Load data in R<\/h2>\n<p>To load <code>Rdata<\/code> in <code>R<\/code> is easy and straightforward method. However, first we need to know how to save the dataframe in R. The function used for saving the dataframe is <code>save(objectlist, file=\"myfile\")<\/code>, where <code>objectlist<\/code> is the name of your current dataframe and <code>myfile<\/code> is the filename of RDATA you will save on your computer. The function to upload the Rdata in R is <code>load()<\/code>.<\/p>\n<p>Save the dataset:<\/p>\n<pre>\r\nsave(df, file=\"mydata.Rdata\")<\/pre>\n<p>Load data in R:<\/p>\n<pre>load(\"mydata.Rdata\")<\/pre>\n<p>Other interesting functions are <code>ls()<\/code> which list the objects in the current workspace, <code>rm(objectlist)<\/code> which delete objects in your workspace.<\/p>\n<p>We finish this post here. Feel free to post a comment if you have any question or suggestion about this post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we will show how to import data from other sources into the R workspace. Import data from SPSS R can import datasets from SPSS with the function read.spss() from the package foreign. Alternatively, the function spss.get() from Hmisc package can be used. While foreign is a default package in R, the Hmisc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1268,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[80,232],"class_list":["post-1265","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-getting-data","tag-import-data","tag-rstats"],"views":68663,"_links":{"self":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/posts\/1265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/comments?post=1265"}],"version-history":[{"count":0,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/posts\/1265\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/media\/1268"}],"wp:attachment":[{"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/media?parent=1265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/categories?post=1265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datascienceplus.com\/wp-json\/wp\/v2\/tags?post=1265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}