r - How Do I Calculate Proportions Of Data Subsets? -
i've got code below need with. i'm asking data science coursera class "are americans financial satisfaction affected previous year's annual s&p500 gain/loss?". i'm trying plot graph amount of observations either satisfied or more or less satisfied, against entire population (a proportion) y axis, , "percentchange"
x-axis. posted entire code further down, in case necessary understand i'm trying do. of these observations in same table "finalresults"
, listed under variable column categorical, named "financialsatisfaction"
. i'm not sure go here, big problem i'm having how calculate proportions based on "percentchange"
in final results table. right below had tried, way off. need filter satisfaction proportions year, x-axis each year's percentage change. appreciated, near knowledgeable enough r figure out.
satisfied <- subset(finalresults, financialsatisfaction == "satisfied") moreorless <- subset(finalresults, financialsatisfaction == "more or less") notatall <- subset(finalresults, financialsatisfaction == "not @ all") myproportion = (satisfied + moreorless) / 29205
full code:
require(quandl) require(lubridate) require(zoo) require(xts) mygss <- load(url("http://bit.ly/dasi_gss_data")) year <- gss$year finsat <- gss$satfin relativetable <- data.frame(year, finsat) relativetable <- subset(relativetable, year > "1988" & !is.na(finsat)) 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 combined <- merge(relativetable, spreturns, by.x = "year", by.y = "year ending") names(combined)[6] <- "percentchange" finalresults <- data.frame(combined$year, combined$finsat, combined$percentchange) names(finalresults)[1] <- "year" names(finalresults)[2] <- "financialsatisfaction" names(finalresults)[3] <- "percentchange" finalresults$percentchange <- finalresults$percentchange * 100 satisfied <- subset(finalresults, financialsatisfaction == "satisfied") moreorless <- subset(finalresults, financialsatisfaction == "more or less") notatall <- subset(finalresults, financialsatisfaction == "not @ all") myproportion <- (satisfied + moreorless) / 29205
in code,
myproportion = (satisfied + moreorless) / 29205
satisfied
, moreorless
data.frame
s, result df; want like
myproporition <- mean(finalresults$financialsatisfaction == "satisfied" | finalresults$financialsatisfaction == "more or less")
Comments
Post a Comment