I'm working on a project for my Data Science class. My question for the project is "Are Americans financial satisfaction dependent/affected by the annual return of the S&P500 in the year prior?" This is an observational study. I have broken down information from other datasets so I now have 56,000 cases, with the variables for year and financial satisfaction. I also have the annualized returns of the S&P500 in percent terms from 1971 through 2013.
I have to now take the annual return of 1971 and apply it to ALL variables under the year 1972 in a new column of the dataset called (spReturns).So essentially the returns will always be year-1. I'm new to R and have no idea how to do this so I was hoping I could get some help. My code is below in case you need to replicate it.
install.packages("lubridate")
install.packages("zoo")
install.packages("xts")
install.packages("Quandl")
require(Quandl)
require(lubridate)
require(zoo)
require(xts)
myData <- load(url("http://bit.ly/dasi_gss_data"))
myData <- myData
year <- gss$year
finSat <- gss$satfin
relativeTable <- data.frame(year, finSat)
relativeTable <- subset(relativeTable, year > "1988")
spReturns <- Quandl("SANDP/ANNRETS", trim_start="1970-01-11",
trim_end="2012-12-31", authcode="nwy3a_Gmd7TSS9fVirxT",
collapse="annual")
percentChange <- spReturns$"Total Return Change"
spReturns$"Year Ending" <- format((spReturns$"Year Ending"), "%Y")
spReturns$"Year Ending" <- as.numeric(spReturns$"Year Ending")
spReturns$"Year Ending" <- spReturns[,1] + 1 #the following year