### R programming: Getting GDP by State Data into R R is a popular statistical package that is both powerful and free.  The objective of this post is to examine the different ways that one can import and store data in R.  Specifically this post will cover how to read in numeric and text data by 1) manually imputing the data, 2) reading the data as a vector with the “scan” function, 3) importing CSV files with the “read.table” function, and 4) exporting data in R into a text file.

The data will come from the Bureau of Economic Analysis and consist of total state level GDP for 2010. http://www.bea.gov/regional/gsp/

MANUALLY INPUTTING INTO R

For obvious reasons inputting the data manually would not be preferred, but there may be cases were the manual entry of data may be required.  The following R code inputs the data for the first few states (in alphabetical order) into R: The first line of code with the “#” is ignored by R and merely comments on what the code is doing.  Comments can be useful reminders of why things were done in a program a certain way. The next line of code creates a vector called “gdp” that is used in conjunction with the scan function.  The scan function tells R that data is about to be imported into the “gdp” vector that was created.  Then the values are manually typed in for each observation.  Once we reach the final observation and hit return twice R gives use a message, “Read 3 items”, this shows that the data was read in correctly.  One can know use the vector “gdp” in a program.  The simplest command is to type “gdp” and hit enter; this command prints the data in the vector onto the screen. One can do the same for the names of the states with the following command: In this case the data is not numeric so the (what=” “) function inside the scan function specifies that the vector “state” only contains character data.

IMPORTING CSV DATA INTO R In the code above a data set is created called “stategdp.csv” that is imported using the “read.csv” function.  This function is especially designed to read in comma separated variable files into R.  The parameter “header = T” indicates that the data contains headers for the variable names which in this case are “State” and “Year_2010”.

EXPORTING R DATA INTO A TEXT FILE The code above exports the data state GDP data into a text file.  The default option for the “write.table” function is to export with the name of the rows and quotes for string characters.  It is very common not to want these either of these in the export.  The following code at the end of the file path accomplishes that task.  