Site icon R-bloggers

Playing Around With R

[This article was first published on The Dancing Economist, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
okay so today we will start playing around with R and will use GDP as our ginny pig.
okay so first do the following:

1. DOWNLOAD R
2. Create a folder and call it “R” on your desktop.  Then type in
getwd()
this will produce the current place where R finds stuff (you know the data you need to manipulate)
3. Copy whatever getwd() produced and paste it into the following function setwd()
and type /Desktop/R after it.  It should look as follows:

setwd(“/Users/stevensabol/Desktop/R”)

4. Go to FRED and download a data set into excel, convert the file to a CSV file. Make sure to name it something simple and straight to the point, then save it in your R folder.

5. Then type the following into R:

GDP<-read.csv(“gdp.csv”)

I have GDP because that is the series I am using. If you are using the CPI then type CPI where I typed GDP. Its that simple. Its easy to get confused so make sure you keep it simple.

Okay this next part is confusing because it very well might be redundant, but do it anyway.

6. Type in,

GDP=scan(“/Users/stevensabol/Desktop/R/gdp.csv”)

this seems to actually get the data in right.

7. Then you have to convert the series into a time series so R can understand:

GDP=ts(GDP,start=1,frequency=1)

“Start” denotes where you want your observations to start. for me it is one, because the first observation makes no sense (not that you can see it anyway). Frequency denotes the number of records in the data per observation.

8. At this point it may make sense to plot what you have. So go for it!

plot(GDP,ylab=”REAL GDP”,main=”U.S.”)

ylab denotes the y axis label
main denotes the main label

Here is what my plot looks like:


More lessons will come shortly!

Keep Dancin’

Steven J.

To leave a comment for the author, please follow the link and comment on their blog: The Dancing Economist.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.